add: create crud product
This commit is contained in:
51
src/product/product.service.ts
Normal file
51
src/product/product.service.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
|
||||
import { EntityNotFoundError, Repository } from 'typeorm';
|
||||
import { Product } from './entities/product.entity';
|
||||
import { ProductCategories } from './entities/product-category.entity';
|
||||
import { ProductSubCategories } from './entities/product-sub-category.entity';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { CreateProductDto } from '../product/dto/create-product.dto';
|
||||
import { CreateCategoriesProductDto } from './dto/categories/create-categories-product.dto';
|
||||
import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
constructor(
|
||||
@InjectRepository(Product)
|
||||
private productRepository: Repository<Product>,
|
||||
) {}
|
||||
|
||||
async create(createProductDto: CreateProductDto) {
|
||||
const result = await this.productRepository.insert(createProductDto);
|
||||
|
||||
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
||||
}
|
||||
|
||||
findAll(page) {
|
||||
return this.productRepository.findAndCount({
|
||||
skip: page * 10,
|
||||
take: 10,
|
||||
order: {
|
||||
version: 'DESC',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
try {
|
||||
return await this.productRepository.findOneOrFail(id);
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.NOT_FOUND,
|
||||
error: 'Data not found',
|
||||
},
|
||||
HttpStatus.NOT_FOUND,
|
||||
);
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user