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 { CreateCustomerDto, UpdateCustomerDto, FindManyCustomerDto, FlatQueryCustomerDto, CustomerResponseDto, CustomerIdDto, } from '../dto/customer'; @ApiTags('Customer') @Controller('customer') export class CustomerController { constructor(private readonly prisma: PrismaService) {} @Post() @ApiOperation({ summary: 'Create a new Customer record', operationId: 'createCustomer' }) @ApiBody({ type: CreateCustomerDto }) @ApiResponse({ status: 201, description: 'Created Customer record', type: CustomerResponseDto }) async createCustomer(@Body() data: CreateCustomerDto): Promise { return this.prisma.customer.create({ data }); } @Put(':id') @ApiOperation({ summary: 'Update a Customer record', operationId: 'updateCustomer' }) @ApiBody({ type: UpdateCustomerDto }) @ApiResponse({ status: 200, description: 'Updated Customer record', type: CustomerResponseDto }) @ApiResponse({ status: 404, description: 'Customer record not found' }) async updateCustomer(@Param() params: CustomerIdDto, @Body() data: UpdateCustomerDto): Promise { try { return await this.prisma.customer.update({ where: params, data }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Customer record not found'); } throw error; } } @Get(':id') @ApiOperation({ summary: 'Get a Customer record by ID', operationId: 'getCustomer' }) @ApiResponse({ status: 200, description: 'Customer record', type: CustomerResponseDto }) @ApiResponse({ status: 404, description: 'Customer record not found' }) async getCustomer(@Param() params: CustomerIdDto): Promise { try { return await this.prisma.customer.findUniqueOrThrow({ where: params }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Customer record not found'); } throw error; } } @Get() @ApiOperation({ summary: 'Get a list of Customer records', operationId: 'getCustomerList' }) @ApiResponse({ status: 200, description: 'List of Customer records', type: [CustomerResponseDto] }) async getCustomerList(@Query() query: FlatQueryCustomerDto): Promise { return this.prisma.customer.findMany(query.toPrismaQuery()); } @Post('search') @ApiOperation({ summary: 'Search Customer records', operationId: 'searchCustomer' }) @ApiBody({ type: FindManyCustomerDto }) @ApiResponse({ status: 200, description: 'List of Customer records', type: [CustomerResponseDto] }) async searchCustomer(@Body() query: FindManyCustomerDto): Promise { const { where, orderBy, take, skip } = query; return this.prisma.customer.findMany({ where, orderBy, take, skip, }); } @Delete(':id') @ApiOperation({ summary: 'Delete a Customer record', operationId: 'deleteCustomer' }) @HttpCode(HttpStatus.NO_CONTENT) @ApiResponse({ status: 204, description: 'Customer record deleted' }) @ApiResponse({ status: 404, description: 'Customer record not found' }) async deleteCustomer(@Param() params: CustomerIdDto): Promise { try { await this.prisma.customer.delete({ where: params }); } catch (error) { if (error instanceof PrismaClientKnownRequestError && error.code === 'P2025') { throw new NotFoundException('Customer record not found'); } throw error; } } }