import { Service } from 'typedi' ; import { OrmRepository } from 'typeorm-typedi-extensions' ; import { Logger , LoggerInterface } from '../../decorators/Logger' ; import { UserGroup } from '../models/UserGroup' ; import { UserGroupRepository } from '../repositories/UserGroupRepository' ; import { Like } from 'typeorm' ; @ Service ( ) export class UserGroupService { constructor ( @ OrmRepository ( ) private userGroupRepository , @ Logger ( __filename ) private log ) { } public findOne ( findCondition ) { this . log . info ( 'Find role' ) ; return this . userGroupRepository . findOne ( findCondition ) ; } public list ( limit , offset , select = [ ] , whereConditions = [ ] , count ) { const condition = { } ; if ( select && select . length > 0 ) { condition . select = select ; } condition . where = { } ; if ( whereConditions && whereConditions . length > 0 ) { whereConditions . 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 ; } console . log ( condition ) ; if ( count ) { return this . userGroupRepository . count ( condition ) ; } return this . userGroupRepository . find ( condition ) ; } public async create ( userGroup ) { const newUserGroup = await this . userGroupRepository . save ( userGroup ) ; return newUserGroup ; } public update ( id , userGroup ) { this . log . info ( 'Update a role' ) ; userGroup . groupId = id ; return this . userGroupRepository . save ( userGroup ) ; } public async delete ( id ) { this . log . info ( 'Delete a role' ) ; const deleteUser = await this . userGroupRepository . delete ( id ) ; return deleteUser ; } }