transform core api codes into this project, update modules as admin/pos context modules
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGuildDto {
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
code?: string
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/swagger'
|
||||
import { CreateGuildDto } from './create-guild.dto'
|
||||
|
||||
export class UpdateGuildDto extends PartialType(CreateGuildDto) {}
|
||||
@@ -0,0 +1,20 @@
|
||||
import { ApiProperty, PartialType } from '@nestjs/swagger'
|
||||
import { IsOptional, IsString } from 'class-validator'
|
||||
|
||||
export class CreateGoodCategoryDto {
|
||||
@IsString()
|
||||
@ApiProperty()
|
||||
name: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
description?: string
|
||||
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@ApiProperty({ required: false })
|
||||
image_url?: string
|
||||
}
|
||||
|
||||
export class UpdateGoodCategoryDto extends PartialType(CreateGoodCategoryDto) {}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common'
|
||||
import {
|
||||
CreateGoodCategoryDto,
|
||||
UpdateGoodCategoryDto,
|
||||
} from './dto/create-good-category.dto'
|
||||
import { GoodCategoriesService } from './good-categories.service'
|
||||
|
||||
@Controller('admin/guilds/:guildId/good_categories')
|
||||
export class GoodCategoriesController {
|
||||
constructor(private readonly goodCategoriesService: GoodCategoriesService) {}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('guildId') guildId: string) {
|
||||
return this.goodCategoriesService.findAll(guildId)
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
findOne(@Param('guildId') guildId: string, @Param('id') id: string) {
|
||||
return this.goodCategoriesService.findOne(guildId, id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Param('guildId') guildId: string, @Body() data: CreateGoodCategoryDto) {
|
||||
return this.goodCategoriesService.create(guildId, data)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(
|
||||
@Param('guildId') guildId: string,
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateGoodCategoryDto,
|
||||
) {
|
||||
return this.goodCategoriesService.update(guildId, id, data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GoodCategoriesController } from './good-categories.controller'
|
||||
import { GoodCategoriesService } from './good-categories.service'
|
||||
|
||||
@Module({
|
||||
controllers: [GoodCategoriesController],
|
||||
providers: [GoodCategoriesService],
|
||||
})
|
||||
export class GuildGoodCategoriesModule {}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { GoodCategoryOmit } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import {
|
||||
CreateGoodCategoryDto,
|
||||
UpdateGoodCategoryDto,
|
||||
} from './dto/create-good-category.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodCategoriesService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
private readonly goodCategoriesOmit = {
|
||||
complex_id: true,
|
||||
guild_id: true,
|
||||
is_default_guild_good: true,
|
||||
} as GoodCategoryOmit
|
||||
|
||||
async findAll(guild_id: string) {
|
||||
const categories = await this.prisma.goodCategory.findMany({
|
||||
where: {
|
||||
guild_id,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.list(
|
||||
categories.map(category => {
|
||||
const { _count, ...rest } = category
|
||||
return {
|
||||
...rest,
|
||||
goods_count: Number(_count.goods),
|
||||
}
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(guild_id: string, categoryId: string) {
|
||||
const category = await this.prisma.goodCategory.findUnique({
|
||||
where: {
|
||||
guild_id,
|
||||
id: categoryId,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.single({
|
||||
...category,
|
||||
goods_count: Number(category?._count.goods),
|
||||
})
|
||||
}
|
||||
|
||||
create(guild_id: string, data: CreateGoodCategoryDto) {
|
||||
const category = this.prisma.goodCategory.create({
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
name: data.name,
|
||||
image_url: data.image_url,
|
||||
description: data.description,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.create({ ...category, goods_count: 0 })
|
||||
}
|
||||
|
||||
async update(guild_id: string, id: string, data: UpdateGoodCategoryDto) {
|
||||
const category = await this.prisma.goodCategory.update({
|
||||
where: {
|
||||
guild_id,
|
||||
id,
|
||||
},
|
||||
data: {
|
||||
guild: {
|
||||
connect: {
|
||||
id: guild_id,
|
||||
},
|
||||
},
|
||||
name: data.name,
|
||||
image_url: data.image_url,
|
||||
description: data.description,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
goods: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodCategoriesOmit,
|
||||
})
|
||||
|
||||
return ResponseMapper.update({
|
||||
...category,
|
||||
goods_count: Number(category?._count.goods),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { Body, Controller, Get, Param, Post, Put } from '@nestjs/common'
|
||||
import { ApiTags } from '@nestjs/swagger'
|
||||
import { CreateGuildDto } from './dto/create-guild.dto'
|
||||
import { UpdateGuildDto } from './dto/update-guild.dto'
|
||||
import { GuildsService } from './guilds.service'
|
||||
|
||||
@ApiTags('guilds')
|
||||
@Controller('admin/guilds')
|
||||
export class GuildsController {
|
||||
constructor(private readonly guildsService: GuildsService) {}
|
||||
|
||||
@Get()
|
||||
async findAll() {
|
||||
return this.guildsService.findAll()
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async findOne(@Param('id') id: string) {
|
||||
return this.guildsService.findOne(id)
|
||||
}
|
||||
|
||||
@Post()
|
||||
async create(@Body() data: CreateGuildDto) {
|
||||
return await this.guildsService.create(data)
|
||||
}
|
||||
|
||||
@Put(':id')
|
||||
async update(@Param('id') id: string, @Body() data: UpdateGuildDto) {
|
||||
return await this.guildsService.update(id, data)
|
||||
}
|
||||
|
||||
// @Delete(':id')
|
||||
// async delete(@Param('id') id: string) {
|
||||
// await this.guildsService.delete(id)
|
||||
// return ResponseMapper.delete()
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GuildGoodCategoriesModule } from './good-categories/good-categories.module'
|
||||
import { GuildGoodsModule } from './goods/goods.module'
|
||||
import { GuildsController } from './guilds.controller'
|
||||
import { GuildsService } from './guilds.service'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, GuildGoodsModule, GuildGoodCategoriesModule],
|
||||
providers: [GuildsService],
|
||||
controllers: [GuildsController],
|
||||
})
|
||||
export class AdminGuildsModule {}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { CreateGuildDto } from './dto/create-guild.dto'
|
||||
import { UpdateGuildDto } from './dto/update-guild.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GuildsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async findAll() {
|
||||
const [guilds, count] = await this.prisma.$transaction([
|
||||
this.prisma.guild.findMany({
|
||||
include: {
|
||||
_count: {
|
||||
select: {
|
||||
business_activities: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
this.prisma.guild.count(),
|
||||
])
|
||||
|
||||
return ResponseMapper.paginate(
|
||||
guilds.map(guild => {
|
||||
const { _count, ...rest } = guild
|
||||
return {
|
||||
...rest,
|
||||
businessActivitiesCount: guild._count.business_activities,
|
||||
}
|
||||
}),
|
||||
{ count },
|
||||
)
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const guild = await this.prisma.guild.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
business_activities: {
|
||||
omit: {
|
||||
guild_id: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return ResponseMapper.single(guild)
|
||||
}
|
||||
|
||||
async create(data: CreateGuildDto) {
|
||||
const guild = await this.prisma.guild.create({ data })
|
||||
return ResponseMapper.create(guild)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateGuildDto) {
|
||||
const guild = await this.prisma.guild.update({ where: { id }, data })
|
||||
return ResponseMapper.update(guild)
|
||||
}
|
||||
|
||||
// async delete(id: string) {
|
||||
// await this.prisma.guild.delete({ where: { id } })
|
||||
// return ResponseMapper.
|
||||
// }
|
||||
}
|
||||
Reference in New Issue
Block a user