import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import Reserve from '../../../db/entities/Reserve' ; import IReserveDB from '../../db/IReserveDB' ; import IIPCClient from '../../ipc/IIPCClient' ; import IReserveApiModel from './IReserveApiModel' ; @ injectable ( ) export default class ReserveApiModel implements IReserveApiModel { private ipc : IIPCClient ; private reserveDB : IReserveDB ; constructor ( @ inject ( 'IIPCClient' ) ipc , @ inject ( 'IReserveDB' ) reserveDB ) { this . ipc = ipc ; this . reserveDB = reserveDB ; } param option return public add ( option ) { return this . ipc . reserveation . add ( option ) ; } param reserveId param option return public async edit ( reserveId , option ) { return this . ipc . reserveation . edit ( reserveId , option ) ; } param reserveId param isHalfWidth return public async get ( reserveId , isHalfWidth ) { const reserve = await this . reserveDB . findId ( reserveId ) ; return reserve === null ? null : this . toReserveItem ( reserve , isHalfWidth ) ; } param option return public async gets ( option ) { const [ reserves , total ] = await this . reserveDB . findAll ( option ) ; return { reserves : reserves . map ( r => { return this . toReserveItem ( r , option . isHalfWidth ) ; } ) , total , } ; } param reserves param isHalfWidth return private toReserveItem ( reserve , isHalfWidth ) { const item = { id : reserve . id , isSkip : reserve . isSkip , isConflict : reserve . isConflict , isOverlap : reserve . isOverlap , allowEndLack : reserve . allowEndLack , isTimeSpecified : reserve . isTimeSpecified , isDeleteOriginalAfterEncode : reserve . isDeleteOriginalAfterEncode , channelId : reserve . channelId , startAt : reserve . startAt , endAt : reserve . endAt , name : isHalfWidth ? reserve . halfWidthName : reserve . name , } ; if ( reserve . ruleId !== null ) { item . ruleId = reserve . ruleId ; } if ( reserve . tags !== null ) { item . tags = JSON . parse ( reserve . tags ) ; } if ( reserve . parentDirectoryName !== null ) { item . parentDirectoryName = reserve . parentDirectoryName ; } if ( reserve . directory !== null ) { item . directory = reserve . directory ; } if ( reserve . recordedFormat !== null ) { item . recordedFormat = reserve . recordedFormat ; } if ( reserve . encodeMode1 !== null ) { item . encodeMode1 = reserve . encodeMode1 ; } if ( reserve . encodeParentDirectoryName1 !== null ) { item . encodeParentDirectoryName1 = reserve . encodeParentDirectoryName1 ; } if ( reserve . encodeDirectory1 !== null ) { item . encodeDirectory1 = reserve . encodeDirectory1 ; } if ( reserve . encodeMode2 !== null ) { item . encodeMode2 = reserve . encodeMode2 ; } if ( reserve . encodeParentDirectoryName2 !== null ) { item . encodeParentDirectoryName2 = reserve . encodeParentDirectoryName2 ; } if ( reserve . encodeDirectory3 !== null ) { item . encodeDirectory3 = reserve . encodeDirectory3 ; } if ( reserve . encodeMode3 !== null ) { item . encodeMode3 = reserve . encodeMode3 ; } if ( reserve . encodeParentDirectoryName3 !== null ) { item . encodeParentDirectoryName3 = reserve . encodeParentDirectoryName3 ; } if ( reserve . encodeDirectory3 !== null ) { item . encodeDirectory3 = reserve . encodeDirectory3 ; } if ( reserve . programId !== null ) { item . programId = reserve . programId ; } if ( reserve . description !== null ) { if ( isHalfWidth === true ) { if ( reserve . halfWidthDescription !== null ) { item . description = reserve . halfWidthDescription ; } } else { item . description = reserve . description ; } } if ( reserve . extended !== null ) { if ( isHalfWidth === true ) { if ( reserve . halfWidthExtended !== null ) { item . extended = reserve . halfWidthExtended ; } } else { item . extended = reserve . extended ; } } if ( reserve . rawExtended !== null ) { if ( isHalfWidth === true ) { if ( reserve . rawHalfWidthExtended !== null ) { item . rawExtended = JSON . parse ( reserve . rawHalfWidthExtended ) ; } } else { item . rawExtended = JSON . parse ( reserve . rawExtended ) ; } } if ( reserve . genre1 !== null ) { item . genre1 = reserve . genre1 ; } if ( reserve . subGenre1 !== null ) { item . subGenre1 = reserve . subGenre1 ; } if ( reserve . genre2 !== null ) { item . genre2 = reserve . genre2 ; } if ( reserve . subGenre2 !== null ) { item . subGenre2 = reserve . subGenre2 ; } if ( reserve . genre3 !== null ) { item . genre3 = reserve . genre3 ; } if ( reserve . subGenre3 !== null ) { item . subGenre3 = reserve . subGenre3 ; } if ( reserve . videoType !== null ) { item . videoType = < any > reserve . videoType ; } if ( reserve . videoResolution !== null ) { item . videoResolution = < any > reserve . videoResolution ; } if ( reserve . videoStreamContent !== null ) { item . videoStreamContent = reserve . videoStreamContent ; } if ( reserve . videoComponentType !== null ) { item . videoComponentType = reserve . videoComponentType ; } if ( reserve . audioSamplingRate !== null ) { item . audioSamplingRate = < any > reserve . audioSamplingRate ; } return item ; } param option return public async getLists ( option ) { const reserves = await this . reserveDB . findLists ( option ) ; const result = { normal : [ ] , conflicts : [ ] , skips : [ ] , overlaps : [ ] , } ; for ( const reserve of reserves ) { const item = this . toReserveListItem ( reserve ) ; if ( reserve . isConflict === true ) { result . conflicts . push ( item ) ; } else if ( reserve . isSkip === true ) { result . skips . push ( item ) ; } else if ( reserve . isOverlap === true ) { result . overlaps . push ( item ) ; } else { result . normal . push ( item ) ; } } return result ; } return public async getCnts ( ) { const reserves = await this . reserveDB . findLists ( ) ; const result = { normal : 0 , conflicts : 0 , skips : 0 , overlaps : 0 , } ; for ( const reserve of reserves ) { if ( reserve . isConflict === true ) { result . conflicts ++ ; } else if ( reserve . isSkip === true ) { result . skips ++ ; } else if ( reserve . isOverlap === true ) { result . overlaps ++ ; } else { result . normal ++ ; } } return result ; } param reserve return private toReserveListItem ( reserve ) { const result = { reserveId : reserve . id , } ; if ( reserve . programId !== null ) { result . programId = reserve . programId ; } if ( reserve . ruleId !== null ) { result . ruleId = reserve . ruleId ; } return result ; } param reserveId return public cancel ( reserveId ) { return this . ipc . reserveation . cancel ( reserveId ) ; } param reserveId return public removeSkip ( reserveId ) { return this . ipc . reserveation . removeSkip ( reserveId ) ; } param reserveId return public removeOverlap ( reserveId ) { return this . ipc . reserveation . removeOverlap ( reserveId ) ; } return public updateAll ( ) { return this . ipc . reserveation . updateAll ( false ) ; } }