import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import RecordedTag from '../../../db/entities/RecordedTag' ; import StrUtil from '../../../util/StrUtil' ; import IRecordedTagDB from '../../db/IRecordedTagDB' ; import IRecordedTagEvent from '../../event/IRecordedTagEvent' ; import ILogger from '../../ILogger' ; import ILoggerModel from '../../ILoggerModel' ; import IRecordedTagManadeModel from './IRecordedTagManadeModel' ; @ injectable ( ) export default class RecordedTagManadeModel implements IRecordedTagManadeModel { private log : ILogger ; private recordedTagDB : IRecordedTagDB ; private recordedTagEvent : IRecordedTagEvent ; constructor ( @ inject ( 'ILoggerModel' ) logger , @ inject ( 'IRecordedTagDB' ) recordedTagDB , @ inject ( 'IRecordedTagEvent' ) recordedTagEvent ) { this . log = logger . getLogger ( ) ; this . recordedTagDB = recordedTagDB ; this . recordedTagEvent = recordedTagEvent ; } param name param color return public async create ( name , color ) { const newTag = new RecordedTag ( ) ; newTag . name = name ; newTag . color = color ; newTag . halfWidthName = StrUtil . toHalf ( name ) ; const tagId = await this . recordedTagDB . insertOnce ( newTag ) . catch ( err => { this . log . system . error ( `create tag error: ${ name }` ) ; throw err ; } ) ; newTag . id = tagId ; this . log . system . info ( `create tag name: ${ name } id: ${ tagId }` ) ; this . recordedTagEvent . emitCreated ( newTag ) ; return tagId ; } param tagId param name param color return public async update ( tagId , name , color ) { await this . recordedTagDB . updateOnce ( tagId , name , color ) ; this . log . system . info ( `update tag name tagId: ${ tagId }, name: ${ name }` ) ; this . recordedTagEvent . emitUpdated ( tagId ) ; } param tagId param recordedId return public async setRelation ( tagId , recordedId ) { await this . recordedTagDB . setRelation ( tagId , recordedId ) . catch ( err => { this . log . system . error ( `set tag relation error tagId: ${ tagId } recordedId: ${ recordedId }` ) ; throw err ; } ) ; this . log . system . info ( `set tag relation tagId: ${ tagId } recordedId: ${ recordedId }` ) ; this . recordedTagEvent . emitRelated ( tagId , recordedId ) ; } param tagId return public async delete ( tagId ) { await this . recordedTagDB . deleteOnce ( tagId ) . catch ( err => { this . log . system . error ( `delete tag error: ${ name }` ) ; throw err ; } ) ; this . log . system . info ( `delete tag id: ${ tagId }` ) ; this . recordedTagEvent . emitDeleted ( tagId ) ; } param tagId param recordedId return public async deleteRelation ( tagId , recordedId ) { await this . recordedTagDB . deleteRelation ( tagId , recordedId ) . catch ( err => { this . log . system . error ( `delete tag relation error tagId: ${ tagId } recordedId: ${ recordedId }` ) ; throw err ; } ) ; this . log . system . info ( `delete tag relation tagId: ${ tagId } recordedId: ${ recordedId }` ) ; this . recordedTagEvent . emitDeletedRelation ( tagId , recordedId ) ; } }