debugging and add image uploader to guild good
This commit is contained in:
@@ -1,5 +1,17 @@
|
||||
import { Body, Controller, Get, Param, Post } from '@nestjs/common'
|
||||
import { CreateGuildGoodDto } from './dto/create-good.dto'
|
||||
import { multerImageOptions } from '@/multer.config'
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
Param,
|
||||
Patch,
|
||||
Post,
|
||||
UploadedFile,
|
||||
UseInterceptors,
|
||||
} from '@nestjs/common'
|
||||
import { FileInterceptor } from '@nestjs/platform-express'
|
||||
import { ApiBody, ApiConsumes } from '@nestjs/swagger'
|
||||
import { CreateGuildGoodDto, UpdateGuildGoodDto } from './dto/create-good.dto'
|
||||
import { GoodsService } from './goods.service'
|
||||
|
||||
@Controller('admin/guilds/:guildId/goods')
|
||||
@@ -17,7 +29,36 @@ export class GoodsController {
|
||||
}
|
||||
|
||||
@Post()
|
||||
create(@Body() data: CreateGuildGoodDto) {
|
||||
return this.goodsService.create(data)
|
||||
@UseInterceptors(FileInterceptor('file', multerImageOptions))
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@ApiBody({
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
image: { type: 'string', format: 'binary' },
|
||||
},
|
||||
},
|
||||
})
|
||||
create(@Body() data: CreateGuildGoodDto, @UploadedFile() file: Express.Multer.File) {
|
||||
return this.goodsService.create(data, file)
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@UseInterceptors(FileInterceptor('image', multerImageOptions))
|
||||
@ApiConsumes('multipart/form-data')
|
||||
@ApiBody({
|
||||
schema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
image: { type: 'string', format: 'binary' },
|
||||
},
|
||||
},
|
||||
})
|
||||
update(
|
||||
@Param('id') id: string,
|
||||
@Body() data: UpdateGuildGoodDto,
|
||||
@UploadedFile() file: Express.Multer.File,
|
||||
) {
|
||||
return this.goodsService.update(id, data, file)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { UploaderModule } from '@/modules/uploader/uploader.module'
|
||||
import { PrismaModule } from '@/prisma/prisma.module'
|
||||
import { Module } from '@nestjs/common'
|
||||
import { GoodsController } from './goods.controller'
|
||||
@@ -5,7 +6,7 @@ import { GoodsService } from './goods.service'
|
||||
import { GuildGoodImagesModule } from './images/images.module'
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule, GuildGoodImagesModule],
|
||||
imports: [PrismaModule, GuildGoodImagesModule, UploaderModule],
|
||||
controllers: [GoodsController],
|
||||
providers: [GoodsService],
|
||||
})
|
||||
|
||||
@@ -1,39 +1,46 @@
|
||||
import { GoodOmit } from '@/generated/prisma/models'
|
||||
import { UploadedFileTypes } from '@/common/enums/enums'
|
||||
import { GoodSelect, GoodWhereInput } from '@/generated/prisma/models'
|
||||
import { UploaderService } from '@/modules/uploader/uploader.service'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from 'common/response/response-mapper'
|
||||
import { CreateGuildGoodDto } from './dto/create-good.dto'
|
||||
import { CreateGuildGoodDto, UpdateGuildGoodDto } from './dto/create-good.dto'
|
||||
|
||||
@Injectable()
|
||||
export class GoodsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
constructor(
|
||||
private prisma: PrismaService,
|
||||
private readonly uploaderService: UploaderService,
|
||||
) {}
|
||||
|
||||
private readonly goodOmit = {
|
||||
complex_id: true,
|
||||
barcode: true,
|
||||
base_sale_price: true,
|
||||
private readonly defaultSelect: GoodSelect = {
|
||||
id: true,
|
||||
name: true,
|
||||
pricing_model: true,
|
||||
unit_type: true,
|
||||
image_url: true,
|
||||
sku: true,
|
||||
description: true,
|
||||
category: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
private readonly defaultWhere = (guild_id: string): GoodWhereInput => ({
|
||||
is_default_guild_good: true,
|
||||
local_sku: true,
|
||||
} as GoodOmit
|
||||
category: {
|
||||
guild_id,
|
||||
},
|
||||
})
|
||||
|
||||
async findAll(guildId: string) {
|
||||
const [goods, count] = await this.prisma.$transaction([
|
||||
this.prisma.good.findMany({
|
||||
where: {
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id: guildId,
|
||||
},
|
||||
},
|
||||
include: {
|
||||
category: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
where: this.defaultWhere(guildId),
|
||||
select: this.defaultSelect,
|
||||
}),
|
||||
this.prisma.good.count(),
|
||||
])
|
||||
@@ -46,41 +53,81 @@ export class GoodsService {
|
||||
const good = await this.prisma.good.findUnique({
|
||||
where: {
|
||||
id,
|
||||
is_default_guild_good: true,
|
||||
category: {
|
||||
guild_id,
|
||||
},
|
||||
...(this.defaultWhere(guild_id) as any),
|
||||
},
|
||||
|
||||
include: {
|
||||
category: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
|
||||
omit: this.goodOmit,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
|
||||
return ResponseMapper.single(good)
|
||||
}
|
||||
|
||||
async create(data: CreateGuildGoodDto) {
|
||||
async create(data: CreateGuildGoodDto, file: Express.Multer.File) {
|
||||
const { category_id, ...rest } = data
|
||||
|
||||
const good = await this.prisma.good.create({
|
||||
data: {
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
const good = await this.prisma.$transaction(async tx => {
|
||||
let image_url = ''
|
||||
if (file) {
|
||||
const uploadedUrl = await this.uploaderService.uploadFile(
|
||||
file,
|
||||
UploadedFileTypes.GOOD,
|
||||
)
|
||||
image_url = uploadedUrl || ''
|
||||
}
|
||||
|
||||
return await tx.good.create({
|
||||
data: {
|
||||
...rest,
|
||||
is_default_guild_good: true,
|
||||
image_url,
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
...rest,
|
||||
is_default_guild_good: true,
|
||||
},
|
||||
omit: this.goodOmit,
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
})
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
}
|
||||
|
||||
async update(id: string, data: UpdateGuildGoodDto, file: Express.Multer.File) {
|
||||
const { category_id, ...rest } = data
|
||||
|
||||
const good = await this.prisma.$transaction(async tx => {
|
||||
let image_url = ''
|
||||
if (file) {
|
||||
const uploadedUrl = await this.uploaderService.uploadFile(
|
||||
file,
|
||||
UploadedFileTypes.GOOD,
|
||||
)
|
||||
image_url = uploadedUrl || ''
|
||||
}
|
||||
|
||||
const prevImage = await tx.good.findUnique({
|
||||
where: { id },
|
||||
select: { image_url: true },
|
||||
})
|
||||
|
||||
if (image_url && prevImage?.image_url) {
|
||||
this.uploaderService.deleteFile(prevImage.image_url, UploadedFileTypes.GOOD)
|
||||
}
|
||||
|
||||
return await tx.good.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...rest,
|
||||
is_default_guild_good: true,
|
||||
image_url,
|
||||
category: {
|
||||
connect: {
|
||||
id: category_id,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: this.defaultSelect,
|
||||
})
|
||||
})
|
||||
|
||||
return ResponseMapper.create(good)
|
||||
|
||||
@@ -23,7 +23,10 @@ export class GuildGoodImagesService {
|
||||
const url = await this.uploadFileService.uploadFile(file, UploadedFileTypes.GOOD)
|
||||
if (url) {
|
||||
if (currentImage.image_url) {
|
||||
this.uploadFileService.deleteFile(currentImage.image_url)
|
||||
this.uploadFileService.deleteFile(
|
||||
currentImage.image_url,
|
||||
UploadedFileTypes.GOOD,
|
||||
)
|
||||
}
|
||||
return this.prisma.good.update({
|
||||
where: {
|
||||
|
||||
Reference in New Issue
Block a user