import { Service } from 'typedi' ; import { OrmRepository } from 'typeorm-typedi-extensions' ; import { Logger , LoggerInterface } from '../../decorators/Logger' ; import { Category } from '../models/CategoryModel' ; import { CategoryRepository } from '../repositories/CategoryRepository' ; import { Like } from 'typeorm/index' ; @ Service ( ) export class CategoryService { constructor ( @ OrmRepository ( ) private categoryRepository , @ Logger ( __filename ) private log ) { } public async create ( category ) { this . log . info ( 'Create a new category => ' , category . toString ( ) ) ; return this . categoryRepository . save ( category ) ; } public findOne ( category ) { return this . categoryRepository . findOne ( category ) ; } public async delete ( id ) { this . log . info ( 'Delete a user' ) ; await this . categoryRepository . delete ( id ) ; return ; } public list ( limit , offset , select = [ ] , search = [ ] , whereConditions = [ ] , sortOrder , count ) { const condition = { } ; if ( select && select . length > 0 ) { condition . select = select ; } condition . where = { } ; if ( whereConditions && whereConditions . length > 0 ) { whereConditions . forEach ( ( item ) => { condition . where [ item . name ] = item . value ; } ) ; } if ( search && search . length > 0 ) { search . forEach ( ( table ) => { const operator = table . op ; if ( operator === 'where' && table . value !== undefined ) { condition . where [ table . name ] = table . value ; } else if ( operator === 'like' && table . value !== undefined ) { condition . where [ table . name ] = Like ( '%' + table . value + '%' ) ; } } ) ; } if ( limit && limit > 0 ) { condition . take = limit ; condition . skip = offset ; } condition . order = { sortOrder : ( sortOrder === 2 ) ? 'DESC' : 'ASC' , createdDate : 'DESC' } ; if ( count ) { return this . categoryRepository . count ( condition ) ; } return this . categoryRepository . find ( condition ) ; } public find ( category ) { return this . categoryRepository . find ( category ) ; } }