import { BadRequestException , UnprocessableEntityException , NotFoundException } from '@nestjs/common' ; import { Repository , EntityRepository } from 'typeorm' ; import { CategoriesEntity } from '../Entities/categories.entity' ; import { CreateCategoryDto } from 'src/v1/Category/Dto/create-category.dto' ; import { UpdateCategoryDto } from 'src/v1/Category/Dto/update-category.dto' ; @ EntityRepository ( CategoriesEntity ) export class CategoriesRepository extends Repository < CategoriesEntity > { async getCategory ( categoryId ) { try { const category = await this . findOneOrFail ( categoryId ) ; return category ; } catch ( err ) { throw new NotFoundException ( 'Category could not find by given id' ) ; } } async getMainCategories ( ) { const [ categories , total ] = await this . findAndCount ( { where : { parent_category : null } , order : { name : 'ASC' } } ) ; return { categories , count : total } ; } async getChildCategories ( categoryId ) { const [ categories , total ] = await this . findAndCount ( { where : { parent_category : categoryId , } , order : { is_leaf : 'ASC' , name : 'ASC' } } ) ; return { categories , count : total } ; } async getCategoryListByIds ( idList ) { const [ categories , total ] = await this . findAndCount ( { where : { '_id' : { $in : idList } } , skip : 0 , } ) ; return { categories , count : total } ; } async createCategory ( dto ) { const categoryPayload = { name : dto . categoryName , is_leaf : dto . isLeaf , ancestors : [ ] } ; if ( dto . parentCategoryId ) { try { const parentCategory = await this . findOneOrFail ( dto . parentCategoryId ) ; categoryPayload . parent_category = dto . parentCategoryId ; categoryPayload . ancestors . push ( ... parentCategory . ancestors , dto . parentCategoryId ) ; } catch ( err ) { throw new NotFoundException ( 'Category could not find by given id' ) ; } } const newCategory = new CategoriesEntity ( categoryPayload ) ; try { return await this . save ( newCategory ) ; } catch ( err ) { throw new UnprocessableEntityException ( err . errmsg ) ; } } async updateCategory ( categoryId , dto ) { let parentCategory = null ; if ( dto . parentCategoryId ) { try { parentCategory = await this . findOneOrFail ( dto . parentCategoryId ) ; } catch ( err ) { throw new NotFoundException ( 'Parent category could not find by given id' ) ; } } let category ; try { category = await this . findOneOrFail ( categoryId ) ; } catch { throw new NotFoundException ( 'Category could not find by given id' ) ; } try { if ( dto . categoryName ) category . name = dto . categoryName ; if ( dto . isLeaf !== undefined ) category . is_leaf = dto . isLeaf ; if ( parentCategory ) { category . parent_category = dto . parentCategoryId ; category . ancestors = [ ... parentCategory . ancestors , String ( parentCategory . id ) ] ; } await this . save ( category ) ; return category ; } catch ( err ) { throw new BadRequestException ( err . errmsg ) ; } } async deleteCategory ( categoryId ) { let category ; try { category = await this . findOneOrFail ( categoryId ) ; await this . delete ( category ) ; } catch ( err ) { throw new NotFoundException ( 'Category could not find by given id' ) ; } const childCategories = await this . find ( { where : { ancestors : { $in : [ String ( category . id ) ] } } , } ) ; await this . remove ( childCategories ) ; } }