import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { EntityNotFoundError, Repository } from 'typeorm'; import { ProductSubCategories } from './entities/product-sub-category.entity'; import { InjectRepository } from '@nestjs/typeorm'; import { CreateSubCategoriesProductDto } from './dto/sub-categories/create-sub-categories-product.dto'; import { UpdateSubCategoriesProductDto } from './dto/sub-categories/update-sub-categories-product.dto'; import { ProductCategoriesService } from './product-categories.service'; @Injectable() export class ProductSubCategoriesService { constructor( @InjectRepository(ProductSubCategories) private productSubCategoriesRepository: Repository, private productCategoriesService: ProductCategoriesService, ) {} async create(createSubCategoriesProductDto: CreateSubCategoriesProductDto) { const check = await this.productSubCategoriesRepository.findOne({ code: createSubCategoriesProductDto.code, }); if (check) { throw new HttpException( { statusCode: HttpStatus.NOT_ACCEPTABLE, error: 'Sub Category Already Exist', }, HttpStatus.NOT_FOUND, ); } const categories = await this.productCategoriesService.findOne( createSubCategoriesProductDto.categoryId, ); const result = await this.productSubCategoriesRepository.insert({ name: createSubCategoriesProductDto.name, code: createSubCategoriesProductDto.code, category: categories, }); return this.productSubCategoriesRepository.findOneOrFail( result.identifiers[0].id, ); } async findAll(page, category) { const baseQuery = this.productSubCategoriesRepository .createQueryBuilder('product_sub_categories') .leftJoinAndSelect('product_sub_categories.category', 'category'); if (category != 'null' && category) { baseQuery.where({ category: category, }); } const data = await baseQuery .skip(page * 10) .take(10) .getMany(); const totalData = await baseQuery.getCount(); return { data, count: totalData, }; } findAllByCategories(page, category) { return this.productSubCategoriesRepository.findAndCount({ skip: page * 10, take: 10, where: { category: category, }, relations: ['category'], order: { version: 'DESC', }, }); } async findOne(id: string) { try { return await this.productSubCategoriesRepository.findOneOrFail(id); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); } else { throw e; } } } async update( id: string, updateCategoriesProductDto: UpdateSubCategoriesProductDto, ) { try { await this.productSubCategoriesRepository.findOneOrFail(id); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); } else { throw e; } } const categories = await this.productCategoriesService.findOne( updateCategoriesProductDto.categoryId, ); const result = await this.productSubCategoriesRepository.update(id, { name: updateCategoriesProductDto.name, category: categories, }); return this.productSubCategoriesRepository.findOneOrFail(id); } async remove(id: string) { try { await this.productSubCategoriesRepository.findOneOrFail(id); } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( { statusCode: HttpStatus.NOT_FOUND, error: 'Product Sub Categories not found', }, HttpStatus.NOT_FOUND, ); } else { throw e; } } await this.productSubCategoriesRepository.delete(id); } }