import { injectable } from "inversify"; import VideoSource, { IVideoSource, videoSourceType, IVideoSourceResource } from "./"; import { IJsonApiResource } from "../../interfaces"; import Mapper from "../../mapper"; @injectable() export class VideoSourceMapper extends Mapper { toModel(resource: IVideoSourceResource, included: IJsonApiResource[] = []): IVideoSource { const model = new VideoSource(); const attrs = resource.attributes; model.id = resource.id; model.updatedAt = attrs.modify_date; model.createdAt = attrs.create_date; model.container = attrs.container; model.codec = attrs.codec; model.src = attrs.src; model.streamName = attrs.stream_name; model.appName = attrs.app_name; model.width = attrs.width; model.height = attrs.height; model.encodingRate = attrs.encoding_rate; model.size = attrs.size; model.sourceType = attrs.source_type; return model; } toResource(model: IVideoSource): IVideoSourceResource { const resource: IVideoSourceResource = { id: model.id, type: videoSourceType, attributes: { modify_date: model.updatedAt, create_date: model.createdAt, container: model.container, codec: model.codec, src: model.src, stream_name: model.streamName, app_name: model.appName, width: model.width, height: model.height, encoding_rate: model.encodingRate, size: model.size, source_type: model.sourceType, }, }; Object.keys(resource.attributes).forEach((key) => { if (typeof resource.attributes[key] === "undefined") { delete resource.attributes[key]; } }); return resource; } } export default VideoSourceMapper;