Merge branch 'development' into 'devops-staging'

feat: add endpoint product detail

See merge request empatnusabangsa/ppob/ppob-backend!59
This commit is contained in:
Catur Bagaskara 2021-12-23 06:27:37 +00:00
commit 0aeae2f673
2 changed files with 25 additions and 2 deletions

View File

@ -195,7 +195,7 @@ export class ProductController {
@Get(':id') @Get(':id')
async findOne(@Param('id', ParseUUIDPipe) id: string) { async findOne(@Param('id', ParseUUIDPipe) id: string) {
return { return {
data: await this.productService.findOne(id), data: await this.productService.findOneById(id),
statusCode: HttpStatus.OK, statusCode: HttpStatus.OK,
message: 'success', message: 'success',
}; };

View File

@ -1,5 +1,5 @@
import { HttpException, HttpStatus } from '@nestjs/common'; import { HttpException, HttpStatus } from '@nestjs/common';
import { EntityNotFoundError, IsNull, Repository } from 'typeorm'; import { EntityNotFoundError, Repository } from 'typeorm';
import { Product } from './entities/product.entity'; import { Product } from './entities/product.entity';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { CreateProductDto } from './dto/product/create-product.dto'; 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) { async update(id: string, updateProductDto: UpdateProductDto) {
try { try {
await this.productRepository.findOneOrFail(id); await this.productRepository.findOneOrFail(id);