add: crud all product module
This commit is contained in:
@@ -1,22 +1,29 @@
|
||||
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';
|
||||
import { CreateProductDto } from '../product/dto/product/create-product.dto';
|
||||
import { ProductSubCategoriesService } from './product-sub-categories.service';
|
||||
import { UpdateProductDto } from './dto/product/update-product.dto';
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
constructor(
|
||||
@InjectRepository(Product)
|
||||
private productRepository: Repository<Product>,
|
||||
private productSubCategoriesService: ProductSubCategoriesService,
|
||||
) {}
|
||||
|
||||
async create(createProductDto: CreateProductDto) {
|
||||
const result = await this.productRepository.insert(createProductDto);
|
||||
const subCategories = await this.productSubCategoriesService.findOne(
|
||||
createProductDto.subCategoriesId,
|
||||
);
|
||||
|
||||
const result = await this.productRepository.insert({
|
||||
name: createProductDto.name,
|
||||
code: createProductDto.code,
|
||||
status: createProductDto.status,
|
||||
subCategories: subCategories,
|
||||
});
|
||||
|
||||
return this.productRepository.findOneOrFail(result.identifiers[0].id);
|
||||
}
|
||||
@@ -31,9 +38,9 @@ export class ProductService {
|
||||
});
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
async findOne(code: string) {
|
||||
try {
|
||||
return await this.productRepository.findOneOrFail(id);
|
||||
return await this.productRepository.findOneOrFail({ code: code });
|
||||
} catch (e) {
|
||||
if (e instanceof EntityNotFoundError) {
|
||||
throw new HttpException(
|
||||
@@ -48,4 +55,54 @@ export class ProductService {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: string, updateProductDto: UpdateProductDto) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
const subCategories = await this.productSubCategoriesService.findOne(
|
||||
updateProductDto.subCategoriesId,
|
||||
);
|
||||
|
||||
const result = await this.productRepository.update(id, {
|
||||
name: updateProductDto.name,
|
||||
code: updateProductDto.code,
|
||||
status: updateProductDto.status,
|
||||
subCategories: subCategories,
|
||||
});
|
||||
|
||||
return this.productRepository.findOneOrFail(id);
|
||||
}
|
||||
|
||||
async remove(id: string) {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
await this.productRepository.delete(id);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user