import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import Channel from '../../../db/entities/Channel' ; import Program from '../../../db/entities/Program' ; import IChannelDB from '../../db/IChannelDB' ; import IProgramDB , { ProgramWithOverlap } from '../../db/IProgramDB' ; import IScheduleApiModel from './IScheduleApiModel' ; @ injectable ( ) export default class ScheduleApiModel implements IScheduleApiModel { private channelDB : IChannelDB ; private programDB : IProgramDB ; constructor ( @ inject ( 'IChannelDB' ) channelDB , @ inject ( 'IProgramDB' ) programDB ) { this . channelDB = channelDB ; this . programDB = programDB ; } param programId param isHalfWidth return public async getSchedule ( programId , isHalfWidth ) { const program = await this . programDB . findId ( programId ) ; return program === null ? null : this . toScheduleProgramItem ( program , isHalfWidth , true ) ; } param option return public async getSchedules ( option ) { const types = [ ] ; if ( option . GR === true ) { types . push ( 'GR' ) ; } if ( option . BS === true ) { types . push ( 'BS' ) ; } if ( option . CS === true ) { types . push ( 'CS' ) ; } if ( option . SKY === true ) { types . push ( 'SKY' ) ; } if ( types . length === 0 ) { throw new Error ( 'GetScheduleTypesError' ) ; } const channels = await this . channelDB . findChannleTypes ( types , true ) ; const programs = await this . programDB . findSchedule ( { startAt : option . startAt , endAt : option . endAt , isHalfWidth : option . isHalfWidth , types : types , isFree : option . isFree , } ) ; return this . createSchedule ( channels , programs , option . isHalfWidth , ! ! option . needsRawExtended ) ; } param channels param programs param isHalfWidth param needsRawExtended return private createSchedule ( channels , programs , isHalfWidth , needsRawExtended ) { const programsIndex = { } ; for ( const program of programs ) { if ( typeof programsIndex [ program . channelId ] === 'undefined' ) { programsIndex [ program . channelId ] = [ ] ; } programsIndex [ program . channelId ] . push ( this . toScheduleProgramItem ( program , isHalfWidth , needsRawExtended ) ) ; } const result = [ ] ; for ( const channel of channels ) { if ( typeof programsIndex [ channel . id ] === 'undefined' ) { continue ; } result . push ( { channel : this . toScheduleChannleItem ( channel , isHalfWidth ) , programs : programsIndex [ channel . id ] , } ) ; } return result ; } param option return public async getChannelSchedule ( option ) { const channel = await this . channelDB . findId ( option . channelId ) ; if ( channel === null ) { throw new Error ( 'ChannelIsNotFound' ) ; } const programs = [ ] ; let baseTime = option . startAt ; for ( let i = 0 ; i < option . days ; i ++ ) { const p = await this . programDB . findSchedule ( { startAt : baseTime , endAt : baseTime + 60 * 60 * 24 * 1000 , isHalfWidth : option . isHalfWidth , channelId : option . channelId , isFree : option . isFree , } ) ; programs . push ( p ) ; baseTime += 60 * 60 * 24 * 1000 ; } const channelItem = this . toScheduleChannleItem ( channel , option . isHalfWidth ) ; const result = [ ] ; for ( const program of programs ) { result . push ( { channel : channelItem , programs : program . map ( p => { return this . toScheduleProgramItem ( p , option . isHalfWidth , ! ! option . needsRawExtended ) ; } ) , } ) ; } return result ; } param option return public async getBroadcastingSchedule ( option ) { const channels = await this . channelDB . findAll ( true ) ; const programs = await this . programDB . findBroadcasting ( option ) ; return this . createSchedule ( channels , programs , option . isHalfWidth , true ) . map ( s => { if ( s . programs . length > 1 ) { s . programs = [ s . programs [ 0 ] ] ; } return s ; } ) ; } param option param isHalfWidth param limit return public async search ( option , isHalfWidth , limit ? ) { const programs = await this . programDB . findRule ( { searchOption : option , limit : limit , } ) ; return programs . map ( p => { return this . toScheduleProgramItem ( p , isHalfWidth , true ) ; } ) ; } param program param isHalfWidth param needsRawExtended return private toScheduleProgramItem ( program , isHalfWidth , needsRawExtended ) { const result = { id : program . id , channelId : program . channelId , startAt : program . startAt , endAt : program . endAt , isFree : program . isFree , name : isHalfWidth ? program . halfWidthName : program . name , } ; if ( program . description !== null ) { if ( isHalfWidth === true ) { if ( program . halfWidthDescription !== null ) { result . description = program . halfWidthDescription ; } } else { result . description = program . description ; } } if ( program . extended !== null ) { if ( isHalfWidth === true ) { if ( program . halfWidthExtended !== null ) { result . extended = program . halfWidthExtended ; } } else { result . extended = program . extended ; } } if ( needsRawExtended === true && program . rawExtended !== null ) { if ( isHalfWidth === true ) { if ( program . rawHalfWidthExtended !== null ) { result . rawExtended = JSON . parse ( program . rawHalfWidthExtended ) ; } } else { result . rawExtended = JSON . parse ( program . rawExtended ) ; } } if ( program . genre1 !== null ) { result . genre1 = program . genre1 ; } if ( program . subGenre1 !== null ) { result . subGenre1 = program . subGenre1 ; } if ( program . genre2 !== null ) { result . genre2 = program . genre2 ; } if ( program . subGenre2 !== null ) { result . subGenre2 = program . subGenre2 ; } if ( program . genre3 !== null ) { result . genre3 = program . genre3 ; } if ( program . subGenre3 !== null ) { result . subGenre3 = program . subGenre3 ; } if ( program . videoType !== null ) { result . videoType = < any > program . videoType ; } if ( program . videoResolution !== null ) { result . videoResolution = < any > program . videoResolution ; } if ( program . videoStreamContent !== null ) { result . videoStreamContent = program . videoStreamContent ; } if ( program . videoComponentType !== null ) { result . videoComponentType = program . videoComponentType ; } if ( program . audioSamplingRate !== null ) { result . audioSamplingRate = < any > program . audioSamplingRate ; } if ( program . audioComponentType !== null ) { result . audioComponentType = program . audioComponentType ; } return result ; } param channel param isHalfWidth return private toScheduleChannleItem ( channel , isHalfWidth ) { const result = { id : channel . id , serviceId : channel . serviceId , networkId : channel . networkId , name : isHalfWidth ? channel . halfWidthName : channel . name , hasLogoData : channel . hasLogoData , channelType : < any > channel . channelType , } ; if ( channel . remoteControlKeyId !== null ) { result . remoteControlKeyId = channel . remoteControlKeyId ; } return result ; } }