import { Service } from 'typedi' ; import { OrmRepository } from 'typeorm-typedi-extensions' ; import { Logger , LoggerInterface } from '../../decorators/Logger' ; import { Country } from '../models/Country' ; import { CountryRepository } from '../repositories/CountryRepository' ; import { Like } from 'typeorm/index' ; @ Service ( ) export class CountryService { constructor ( @ OrmRepository ( ) private countryRepository , @ Logger ( __filename ) private log ) { } public async create ( country ) { this . log . info ( 'Create a new country ' ) ; return this . countryRepository . save ( country ) ; } public findOne ( country ) { return this . countryRepository . findOne ( country ) ; } public update ( id , country ) { country . countryId = id ; return this . countryRepository . save ( country ) ; } public list ( limit , offset , select = [ ] , search = [ ] , whereConditions = [ ] , 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 !== '' ) { condition . where [ table . name ] = table . value ; } else if ( operator === 'like' && table . value !== '' ) { condition . where [ table . name ] = Like ( '%' + table . value + '%' ) ; } } ) ; } condition . order = { name : 'ASC' , } ; if ( limit && limit > 0 ) { condition . take = limit ; condition . skip = offset ; } if ( count ) { return this . countryRepository . count ( condition ) ; } else { return this . countryRepository . find ( condition ) ; } } public async delete ( id ) { return await this . countryRepository . delete ( id ) ; } }