import { inject , injectable } from 'inversify' ; import * as apid from '../../../api' ; import VideoFile from '../../db/entities/VideoFile' ; import IPromiseRetry from '../IPromiseRetry' ; import IDBOperator from './IDBOperator' ; import IVideoFileDB , { UpdateFilePathOption } from './IVideoFileDB' ; @ injectable ( ) export default class VideoFileDB implements IVideoFileDB { private op : IDBOperator ; private promieRetry : IPromiseRetry ; constructor ( @ inject ( 'IDBOperator' ) op , @ inject ( 'IPromiseRetry' ) promieRetry ) { this . op = op ; this . promieRetry = promieRetry ; } param items return public async restore ( items ) { const connection = await this . op . getConnection ( ) ; const queryRunner = connection . createQueryRunner ( ) ; await queryRunner . startTransaction ( ) ; let hasError = false ; try { await queryRunner . manager . delete ( VideoFile , { } ) ; for ( const item of items ) { await queryRunner . manager . insert ( VideoFile , item ) ; } await queryRunner . commitTransaction ( ) ; } catch ( err ) { console . error ( err ) ; hasError = err ; await queryRunner . rollbackTransaction ( ) ; } finally { await queryRunner . release ( ) ; } if ( hasError ) { throw new Error ( 'restore error' ) ; } } param videoFile return public async insertOnce ( videoFile ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . insert ( ) . into ( VideoFile ) . values ( videoFile ) ; const insertedResult = await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; return insertedResult . identifiers [ 0 ] . id ; } param option return public async updateFilePath ( option ) { const videoFile = await this . findId ( option . videoFileId ) ; if ( videoFile === null ) { throw new Error ( 'VideoFileIsNull' ) ; } const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . update ( VideoFile ) . set ( { parentDirectoryName : option . parentDirectoryName , filePath : option . filePath , } ) . where ( { id : option . videoFileId } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param videoFileId param size return public async updateSize ( videoFileId , size ) { const videoFile = await this . findId ( videoFileId ) ; if ( videoFile === null ) { throw new Error ( 'VideoFileIsNull' ) ; } const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . update ( VideoFile ) . set ( { size : size , } ) . where ( { id : videoFileId } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param VideoFileId return public async deleteOnce ( VideoFileId ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . delete ( ) . from ( VideoFile ) . where ( { id : VideoFileId , } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param recordedId return public async deleteRecordedId ( recordedId ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . createQueryBuilder ( ) . delete ( ) . from ( VideoFile ) . where ( { recordedId : recordedId , } ) ; await this . promieRetry . run ( ( ) => { return queryBuilder . execute ( ) ; } ) ; } param videoFileId return public async findId ( videoFileId ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . getRepository ( VideoFile ) . createQueryBuilder ( ) . where ( { id : videoFileId } ) ; const result = await this . promieRetry . run ( ( ) => { return queryBuilder . getOne ( ) ; } ) ; return typeof result === 'undefined' ? null : result ; } /**
     * 全てのビデオファイルを取得する
     */ public async findAll ( ) { const connection = await this . op . getConnection ( ) ; const queryBuilder = connection . getRepository ( VideoFile ) . createQueryBuilder ( ) ; return await this . promieRetry . run ( ( ) => { return queryBuilder . getMany ( ) ; } ) ; } }