import { Controller, Get, Post, Put, Delete, Body, Param, Query, HttpCode, HttpStatus, NotFoundException } from '@nestjs/common'; import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger'; import { PrismaService } from '../services/prisma.service'; import { PrismaClientKnownRequestError } from '@prisma/client/runtime/library'; import { CreateCategoryDto, UpdateCategoryDto, FindManyCategoryDto, FlatQueryCategoryDto, CategoryResponseDto, CategoryIdDto, } from '../dto/category'; @ApiTags('Category') @Controller('category') export class CategoryController { constructor(private readonly prisma: PrismaService) {} @Post() @ApiOperation({ summary: 'Create a new Category record', operationId: 'createCategory' }) @ApiBody({ type: CreateCategoryDto }) @ApiResponse({ status: 201, description: 'Created Category record', type: CategoryResponseDto }) async createCategory(@Body() data: CreateCategoryDto): Promise { return this.prisma.category.create({ data }); } @Put(':id') @ApiOperation({ summary: 'Update a Category record', operationId: 'updateCategory' }) @ApiBody({ type: UpdateCategoryDto }) @ApiResponse({ status: 200, description: 'Updated Category record', type: CategoryResponseDto }) @ApiResponse({ status: 404, description: 'Category record not found' }) async updateCategory(@Param() params: CategoryIdDto, @Body() data: UpdateCategoryDto): Promise { try { return await this.prisma.category.update({ where: params, data }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Category record not found'); } throw error; } } @Get(':id') @ApiOperation({ summary: 'Get a Category record by ID', operationId: 'getCategory' }) @ApiResponse({ status: 200, description: 'Category record', type: CategoryResponseDto }) @ApiResponse({ status: 404, description: 'Category record not found' }) async getCategory(@Param() params: CategoryIdDto): Promise { try { return await this.prisma.category.findUniqueOrThrow({ where: params }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Category record not found'); } throw error; } } @Get() @ApiOperation({ summary: 'Get a list of Category records', operationId: 'getCategoryList' }) @ApiResponse({ status: 200, description: 'List of Category records', type: [CategoryResponseDto] }) async getCategoryList(@Query() query: FlatQueryCategoryDto): Promise { return this.prisma.category.findMany(query.toPrismaQuery()); } @Post('search') @ApiOperation({ summary: 'Search Category records', operationId: 'searchCategory' }) @ApiBody({ type: FindManyCategoryDto }) @ApiResponse({ status: 200, description: 'List of Category records', type: [CategoryResponseDto] }) async searchCategory(@Body() query: FindManyCategoryDto): Promise { const { where, orderBy, take, skip } = query; return this.prisma.category.findMany({ where, orderBy, take, skip, }); } @Delete(':id') @ApiOperation({ summary: 'Delete a Category record', operationId: 'deleteCategory' }) @HttpCode(HttpStatus.NO_CONTENT) @ApiResponse({ status: 204, description: 'Category record deleted' }) @ApiResponse({ status: 404, description: 'Category record not found' }) async deleteCategory(@Param() params: CategoryIdDto): Promise { try { await this.prisma.category.delete({ where: params }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Category record not found'); } throw error; } } }