feat: add endpoint product detail
This commit is contained in:
parent
1d71ddd2e1
commit
e4c92ef967
|
@ -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',
|
||||||
};
|
};
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user