From e4c92ef9673228ccaea70e30c55efb7346f1840f Mon Sep 17 00:00:00 2001 From: caturbgs Date: Thu, 23 Dec 2021 13:26:52 +0700 Subject: [PATCH] feat: add endpoint product detail --- src/product/product.controller.ts | 2 +- src/product/product.service.ts | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/product/product.controller.ts b/src/product/product.controller.ts index 49b23e9..939e9df 100644 --- a/src/product/product.controller.ts +++ b/src/product/product.controller.ts @@ -195,7 +195,7 @@ export class ProductController { @Get(':id') async findOne(@Param('id', ParseUUIDPipe) id: string) { return { - data: await this.productService.findOne(id), + data: await this.productService.findOneById(id), statusCode: HttpStatus.OK, message: 'success', }; diff --git a/src/product/product.service.ts b/src/product/product.service.ts index cadcdcd..51d7762 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -1,5 +1,5 @@ import { HttpException, HttpStatus } from '@nestjs/common'; -import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; +import { EntityNotFoundError, Repository } from 'typeorm'; import { Product } from './entities/product.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateProductDto } from './dto/product/create-product.dto'; @@ -357,6 +357,29 @@ export class ProductService { } } + async findOneById(id: string) { + try { + return await this.productRepository.findOneOrFail({ + relations: ['supplier'], + where: { + id: id, + }, + }); + } catch (e) { + if (e instanceof EntityNotFoundError) { + throw new HttpException( + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Product not found', + }, + HttpStatus.NOT_FOUND, + ); + } else { + throw e; + } + } + } + async update(id: string, updateProductDto: UpdateProductDto) { try { await this.productRepository.findOneOrFail(id);