import { Service } from 'typedi' ; import { OrmRepository } from 'typeorm-typedi-extensions' ; import { Logger , LoggerInterface } from '../../decorators/Logger' ; import { ProductToCategory } from '../models/ProductToCategory' ; import { ProductToCategoryRepository } from '../repositories/ProductToCategoryRepository' ; import { Like } from 'typeorm' ; @ Service ( ) export class ProductToCategoryService { constructor ( @ OrmRepository ( ) private productToCategoryRepository , @ Logger ( __filename ) private log ) { } public findOne ( findCondition ) { this . log . info ( 'Find all product' ) ; return this . productToCategoryRepository . findOne ( findCondition ) ; } public findAll ( findCondition ) { this . log . info ( 'Find all product' ) ; return this . productToCategoryRepository . find ( findCondition ) ; } public list ( limit , offset , select = [ ] , relation = [ ] , whereConditions = [ ] , price , count ) { const condition = { } ; if ( select && select . length > 0 ) { condition . select = select ; } if ( relation && relation . length > 0 ) { condition . relations = relation ; } condition . where = { } ; if ( whereConditions && whereConditions . length > 0 ) { whereConditions . forEach ( ( item ) => { const operator = item . op ; if ( operator === 'where' && item . value !== '' ) { condition . where [ item . name ] = item . value ; } else if ( operator === 'like' && item . value !== '' ) { condition . where [ item . name ] = Like ( '%' + item . value + '%' ) ; } } ) ; } if ( price && price === 1 ) { condition . order = { price : 'ASC' , } ; } if ( price && price === 2 ) { condition . order = { price : 'DESC' , } ; } if ( limit && limit > 0 ) { condition . take = limit ; condition . skip = offset ; } console . log ( condition ) ; if ( count ) { return this . productToCategoryRepository . count ( condition ) ; } return this . productToCategoryRepository . find ( condition ) ; } public async create ( product ) { const newProduct = await this . productToCategoryRepository . save ( product ) ; return newProduct ; } public update ( id , product ) { this . log . info ( 'Update a product' ) ; product . productId = id ; return this . productToCategoryRepository . save ( product ) ; } public async delete ( id ) { this . log . info ( 'Delete a product' ) ; const newProduct = await this . productToCategoryRepository . delete ( id ) ; return newProduct ; } }