transform core api codes into this project, update modules as admin/pos context modules
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsNumber, IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGoodDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
name: string
|
||||
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
sku: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: true })
|
||||
category_id: string
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
@ApiProperty({ required: false })
|
||||
local_sku?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
barcode?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
description?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
@ApiProperty({ required: false })
|
||||
base_sale_price?: number
|
||||
}
|
||||
|
||||
export class UpdateGoodDto extends PartialType(CreateGoodDto) {}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Controller('admin/guilds/:guildId/goods')
|
||||
export class GoodsController {
|
||||
constructor(private readonly goodsService: GoodsService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('guildId') guildId: string) {
|
||||
return this.goodsService.findAll(guildId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('guildId') guildId: string, @Param('id') goodId: string) {
|
||||
return this.goodsService.findOne(guildId, goodId)
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Param('guildId') guildId: string, @Body() data: CreateGoodDto) {
|
||||
return this.goodsService.create(guildId, data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GoodsController } from './goods.controller'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Module({
|
||||
controllers: [GoodsController],
|
||||
providers: [GoodsService],
|
||||
})
|
||||
export class GuildGoodsModule {}
|
||||
@@ -0,0 +1,73 @@
|
||||
import { GoodOmit } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateGoodDto } from './dto/create-good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly goodOmit = {
|
||||
complex_id: true,
|
||||
barcode: true,
|
||||
base_sale_price: true,
|
||||
is_default_guild_good: true,
|
||||
local_sku: true,
|
||||
guild_id: true,
|
||||
} as GoodOmit
|
||||
|
||||
async findAll(guildId: string) {
|
||||
const [goods, count] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: {
|
||||
guild_id: guildId,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
return ResponseMapper.paginate(goods, {
|
||||
count,
|
||||
})
|
||||
}
|
||||
|
||||
async findOne(guild_id: string, id: string) {
|
||||
const good = await this.prisma.good.findUnique({
|
||||
where: {
|
||||
guild_id,
|
||||
id,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
|
||||
omit: this.goodOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(good)
|
||||
}
|
||||
|
||||
async create(guild_id: string, data: CreateGoodDto) {
|
||||
const { category_id, ...rest } = data
|
||||
|
||||
const good = await this.prisma.good.create({
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
...rest,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user