import { Service } from 'typedi' ; import { OrmRepository } from 'typeorm-typedi-extensions' ; import { Logger , LoggerInterface } from '../../decorators/Logger' ; import { User } from '../models/User' ; import { UserRepository } from '../repositories/UserRepository' ; import { Like } from 'typeorm' ; @ Service ( ) export class UserService { constructor ( @ OrmRepository ( ) private userLoginRepository , @ Logger ( __filename ) private log ) { } public findOne ( findCondition ) { this . log . info ( 'Find all users' ) ; return this . userLoginRepository . findOne ( findCondition ) ; } public list ( limit = 0 , offset = 0 , select = [ ] , relation = [ ] , whereConditions = [ ] , keyword , count ) { console . log ( keyword ) ; 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 ) => { condition . where [ item . name ] = item . value ; } ) ; } if ( keyword ) { condition . where = { firstName : Like ( '%' + keyword + '%' ) , } ; } condition . order = { createdDate : 'DESC' , } ; if ( limit && limit > 0 ) { condition . take = limit ; condition . skip = offset ; } if ( count ) { return this . userLoginRepository . count ( condition ) ; } else { return this . userLoginRepository . find ( condition ) ; } } public async create ( user ) { this . log . info ( 'Create a new user => ' , user . toString ( ) ) ; const newUser = await this . userLoginRepository . save ( user ) ; return newUser ; } public update ( id , user ) { this . log . info ( 'Update a user' ) ; user . userId = id ; return this . userLoginRepository . save ( user ) ; } public async delete ( id ) { this . log . info ( 'Delete a user' ) ; const newUser = await this . userLoginRepository . delete ( id ) ; return newUser ; } public findAll ( findCondition ) { this . log . info ( 'Find all users' ) ; return this . userLoginRepository . find ( findCondition ) ; } }