37 lines
1.4 KiB
TypeScript
37 lines
1.4 KiB
TypeScript
import { injectable } from 'inversify';
|
|
import { Source } from '../../../entities/library/source';
|
|
import { Work } from '../../../entities/library/work';
|
|
import { getDeserializedEntitiesPromise, getDeserializedEntityPromise } from '../get-deserialized';
|
|
import { serializeEntitiesPromise, serializeEntityPromise } from '../serialize-entity-promise';
|
|
import { Serializer } from '../serializer';
|
|
|
|
@injectable()
|
|
export class CopySerializer extends Serializer<CopyEntityInterface, CopySerializedInterface> {
|
|
public async serialize(entity: CopyEntityInterface): Promise<CopySerializedInterface> {
|
|
const [original, sources] = await Promise.all([
|
|
serializeEntityPromise(entity.original),
|
|
serializeEntitiesPromise(entity.sources),
|
|
]);
|
|
|
|
return {
|
|
id: entity.id,
|
|
hash: entity.hash,
|
|
location: entity.location,
|
|
ranking: entity.ranking,
|
|
original,
|
|
sources,
|
|
};
|
|
}
|
|
|
|
public deserialize(partial: Partial<CopySerializedInterface>): Partial<CopyEntityInterface> {
|
|
return {
|
|
...(partial.id ? { id: partial.id } : {}),
|
|
...(partial.hash ? { hash: partial.hash } : {}),
|
|
...(partial.location ? { location: partial.location } : {}),
|
|
...(partial.ranking ? { ranking: partial.ranking } : {}),
|
|
...(partial.original ? { original: getDeserializedEntityPromise(partial.original, Work) } : {}),
|
|
...(partial.sources ? { sources: getDeserializedEntitiesPromise(partial.sources, Source) } : {}),
|
|
};
|
|
}
|
|
}
|