import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import IReserveDB from '../../db/IReserveDB' ; import IRuleDB from '../../db/IRuleDB' ; import IIPCClient from '../../ipc/IIPCClient' ; import IRuleApiModel from './IRuleApiModel' ; @ injectable ( ) export default class RuleApiModel implements IRuleApiModel { private ipc : IIPCClient ; private ruleDB : IRuleDB ; private reserveDB : IReserveDB ; constructor ( @ inject ( 'IIPCClient' ) ipc , @ inject ( 'IRuleDB' ) ruleDB , @ inject ( 'IReserveDB' ) reserveDB ) { this . ipc = ipc ; this . ruleDB = ruleDB ; this . reserveDB = reserveDB ; } param rule return public add ( rule ) { return this . ipc . rule . add ( rule ) ; } param ruleId return public async get ( ruleId ) { return await this . ruleDB . findId ( ruleId , false ) ; } param option return public async gets ( option ) { const [ rules , total ] = await this . ruleDB . findAll ( option ) ; if ( typeof option . type !== 'undefined' ) { const ruleIds = ( rules as apid . Rule [ ] ) . map ( r => { return r . id ; } ) ; if ( ruleIds . length > 0 ) { const reservesCnt = await this . reserveDB . countRuleIds ( ruleIds , option . type ) ; const reserveCntIndex = { } ; for ( const r of reservesCnt ) { reserveCntIndex [ r . ruleId ] = r . ruleIdCnt ; } for ( const rule of rules ) { rule . reservesCnt = typeof reserveCntIndex [ rule . id ] === 'undefined' ? 0 : reserveCntIndex [ rule . id ] ; } } } return { rules , total , } ; } param option return public async searchKeyword ( option ) { return this . ruleDB . findKeyword ( option ) ; } param rule public update ( rule ) { return this . ipc . rule . update ( rule ) ; } param ruleId public enable ( ruleId ) { return this . ipc . rule . enable ( ruleId ) ; } param ruleId public disable ( ruleId ) { return this . ipc . rule . disable ( ruleId ) ; } param ruleId public delete ( ruleId ) { return this . ipc . rule . delete ( ruleId ) ; } param ruleIds return public deletes ( ruleIds ) { return this . ipc . rule . deletes ( ruleIds ) ; } }