RenaiApp/src/main/modules/entity-api/read-controller.ts

275 lines
12 KiB
TypeScript

import type { EntityTarget } from 'typeorm';
import { container, Service } from '../../core/container';
import { getRepository } from '../../core/database';
import { Author } from '../../entities/library/author';
import { AuthorName } from '../../entities/library/author-name';
import { AuthorRole } from '../../entities/library/author-role';
import { AuthorRoleName } from '../../entities/library/author-role-name';
import { CharacterTag } from '../../entities/library/character-tag';
import { Collection } from '../../entities/library/collection';
import { CollectionName } from '../../entities/library/collection-name';
import { CollectionPart } from '../../entities/library/collection-part';
import { Copy } from '../../entities/library/copy';
import { InteractionTag } from '../../entities/library/interaction-tag';
import { Language } from '../../entities/library/language';
import { Site } from '../../entities/library/site';
import { SiteName } from '../../entities/library/site-name';
import { Source } from '../../entities/library/source';
import { Tag } from '../../entities/library/tag';
import { TagName } from '../../entities/library/tag-name';
import { Transformation } from '../../entities/library/transformation';
import { TransformationType } from '../../entities/library/transformation-type';
import { TransformationTypeName } from '../../entities/library/transformation-type-name';
import { Work } from '../../entities/library/work';
import { WorkAuthor } from '../../entities/library/work-author';
import { WorkCharacter } from '../../entities/library/work-character';
import { WorkCharacterName } from '../../entities/library/work-character-name';
import { WorkName } from '../../entities/library/work-name';
import { WorkTag } from '../../entities/library/work-tag';
import { World } from '../../entities/library/world';
import { WorldCharacter } from '../../entities/library/world-character';
import { WorldCharacterName } from '../../entities/library/world-character-name';
import { WorldName } from '../../entities/library/world-name';
import { ipcServer } from '../ipc/ipc-server';
import type { Serializer } from '../serialization/serializer';
type ReadSerializedType = {
[IpcChannel.ENTITY_READ_AUTHOR]: AuthorSerializedInterface;
[IpcChannel.ENTITY_READ_AUTHOR_NAME]: AuthorNameSerializedInterface;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE]: AuthorRoleSerializedInterface;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE_NAME]: AuthorRoleNameSerializedInterface;
[IpcChannel.ENTITY_READ_CHARACTER_TAG]: CharacterTagSerializedInterface;
[IpcChannel.ENTITY_READ_COLLECTION]: CollectionSerializedInterface;
[IpcChannel.ENTITY_READ_COLLECTION_NAME]: CollectionNameSerializedInterface;
[IpcChannel.ENTITY_READ_COLLECTION_PART]: CollectionPartSerializedInterface;
[IpcChannel.ENTITY_READ_COPY]: CopySerializedInterface;
[IpcChannel.ENTITY_READ_INTERACTION_TAG]: InteractionTagSerializedInterface;
[IpcChannel.ENTITY_READ_LANGUAGE]: LanguageSerializedInterface;
[IpcChannel.ENTITY_READ_SITE]: SiteSerializedInterface;
[IpcChannel.ENTITY_READ_SITE_NAME]: SiteNameSerializedInterface;
[IpcChannel.ENTITY_READ_SOURCE]: SourceSerializedInterface;
[IpcChannel.ENTITY_READ_TAG]: TagSerializedInterface;
[IpcChannel.ENTITY_READ_TAG_NAME]: TagNameSerializedInterface;
[IpcChannel.ENTITY_READ_TRANSFORMATION]: TransformationSerializedInterface;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE]: TransformationTypeSerializedInterface;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE_NAME]: TransformationTypeNameSerializedInterface;
[IpcChannel.ENTITY_READ_WORK_AUTHOR]: WorkAuthorSerializedInterface;
[IpcChannel.ENTITY_READ_WORK_CHARACTER]: WorkCharacterSerializedInterface;
[IpcChannel.ENTITY_READ_WORK_CHARACTER_NAME]: WorkCharacterNameSerializedInterface;
[IpcChannel.ENTITY_READ_WORK]: WorkSerializedInterface;
[IpcChannel.ENTITY_READ_WORK_NAME]: WorkNameSerializedInterface;
[IpcChannel.ENTITY_READ_WORK_TAG]: WorkTagSerializedInterface;
[IpcChannel.ENTITY_READ_WORLD]: WorldSerializedInterface;
[IpcChannel.ENTITY_READ_WORLD_NAME]: WorldNameSerializedInterface;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER]: WorldCharacterSerializedInterface;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER_NAME]: WorldCharacterNameSerializedInterface;
};
type ReadIdType = {
[IpcChannel.ENTITY_READ_AUTHOR]: number;
[IpcChannel.ENTITY_READ_AUTHOR_NAME]: number;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE]: number;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE_NAME]: number;
[IpcChannel.ENTITY_READ_CHARACTER_TAG]: number;
[IpcChannel.ENTITY_READ_COLLECTION]: number;
[IpcChannel.ENTITY_READ_COLLECTION_NAME]: number;
[IpcChannel.ENTITY_READ_COLLECTION_PART]: number;
[IpcChannel.ENTITY_READ_COPY]: number;
[IpcChannel.ENTITY_READ_INTERACTION_TAG]: number;
[IpcChannel.ENTITY_READ_LANGUAGE]: string;
[IpcChannel.ENTITY_READ_SITE]: number;
[IpcChannel.ENTITY_READ_SITE_NAME]: number;
[IpcChannel.ENTITY_READ_SOURCE]: number;
[IpcChannel.ENTITY_READ_TAG]: number;
[IpcChannel.ENTITY_READ_TAG_NAME]: number;
[IpcChannel.ENTITY_READ_TRANSFORMATION]: number;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE]: number;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE_NAME]: number;
[IpcChannel.ENTITY_READ_WORK_AUTHOR]: number;
[IpcChannel.ENTITY_READ_WORK_CHARACTER]: number;
[IpcChannel.ENTITY_READ_WORK_CHARACTER_NAME]: number;
[IpcChannel.ENTITY_READ_WORK]: number;
[IpcChannel.ENTITY_READ_WORK_NAME]: number;
[IpcChannel.ENTITY_READ_WORK_TAG]: number;
[IpcChannel.ENTITY_READ_WORLD]: number;
[IpcChannel.ENTITY_READ_WORLD_NAME]: number;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER]: number;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER_NAME]: number;
};
type ReadEntityType = {
[IpcChannel.ENTITY_READ_AUTHOR]: Author;
[IpcChannel.ENTITY_READ_AUTHOR_NAME]: AuthorName;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE]: AuthorRole;
[IpcChannel.ENTITY_READ_AUTHOR_ROLE_NAME]: AuthorRoleName;
[IpcChannel.ENTITY_READ_CHARACTER_TAG]: CharacterTag;
[IpcChannel.ENTITY_READ_COLLECTION]: Collection;
[IpcChannel.ENTITY_READ_COLLECTION_NAME]: CollectionName;
[IpcChannel.ENTITY_READ_COLLECTION_PART]: CollectionPart;
[IpcChannel.ENTITY_READ_COPY]: Copy;
[IpcChannel.ENTITY_READ_INTERACTION_TAG]: InteractionTag;
[IpcChannel.ENTITY_READ_LANGUAGE]: Language;
[IpcChannel.ENTITY_READ_SITE]: Site;
[IpcChannel.ENTITY_READ_SITE_NAME]: SiteName;
[IpcChannel.ENTITY_READ_SOURCE]: Source;
[IpcChannel.ENTITY_READ_TAG]: Tag;
[IpcChannel.ENTITY_READ_TAG_NAME]: TagName;
[IpcChannel.ENTITY_READ_TRANSFORMATION]: Transformation;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE]: TransformationType;
[IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE_NAME]: TransformationTypeName;
[IpcChannel.ENTITY_READ_WORK_AUTHOR]: WorkAuthor;
[IpcChannel.ENTITY_READ_WORK_CHARACTER]: WorkCharacter;
[IpcChannel.ENTITY_READ_WORK_CHARACTER_NAME]: WorkCharacterName;
[IpcChannel.ENTITY_READ_WORK]: Work;
[IpcChannel.ENTITY_READ_WORK_NAME]: WorkName;
[IpcChannel.ENTITY_READ_WORK_TAG]: WorkTag;
[IpcChannel.ENTITY_READ_WORLD]: World;
[IpcChannel.ENTITY_READ_WORLD_NAME]: WorldName;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER]: WorldCharacter;
[IpcChannel.ENTITY_READ_WORLD_CHARACTER_NAME]: WorldCharacterName;
};
async function read<T extends keyof ReadSerializedType>(
id: ReadIdType[T],
entityTarget: EntityTarget<ReadEntityType[T]>,
// @ts-ignore -- yeah, i don't get it
serializer: Serializer<ReadEntityType[T], ReadSerializedType[T], ReadIdType[T]>,
): Promise<ReadSerializedType[T]> {
const repository = await getRepository(entityTarget);
const entity = await repository.findOneOrFail(id);
return serializer.serialize(entity);
}
ipcServer.answer(IpcChannel.ENTITY_READ_AUTHOR, (id) =>
read<IpcChannel.ENTITY_READ_AUTHOR>(id, Author, container.get(Service.AUTHOR_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_AUTHOR_NAME, (id) =>
read<IpcChannel.ENTITY_READ_AUTHOR_NAME>(id, AuthorName, container.get(Service.AUTHOR_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_AUTHOR_ROLE, (id) =>
read<IpcChannel.ENTITY_READ_AUTHOR_ROLE>(id, AuthorRole, container.get(Service.AUTHOR_ROLE_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_AUTHOR_ROLE_NAME, (id) =>
read<IpcChannel.ENTITY_READ_AUTHOR_ROLE_NAME>(id, AuthorRoleName, container.get(Service.AUTHOR_ROLE_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_CHARACTER_TAG, (id) =>
read<IpcChannel.ENTITY_READ_CHARACTER_TAG>(id, CharacterTag, container.get(Service.CHARACTER_TAG_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_COLLECTION, (id) =>
read<IpcChannel.ENTITY_READ_COLLECTION>(id, Collection, container.get(Service.COLLECTION_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_COLLECTION_NAME, (id) =>
read<IpcChannel.ENTITY_READ_COLLECTION_NAME>(id, CollectionName, container.get(Service.COLLECTION_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_COLLECTION_PART, (id) =>
read<IpcChannel.ENTITY_READ_COLLECTION_PART>(id, CollectionPart, container.get(Service.COLLECTION_PART_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_COPY, (id) =>
read<IpcChannel.ENTITY_READ_COPY>(id, Copy, container.get(Service.COPY_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_INTERACTION_TAG, (id) =>
read<IpcChannel.ENTITY_READ_INTERACTION_TAG>(id, InteractionTag, container.get(Service.INTERACTION_TAG_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_LANGUAGE, (code) =>
read<IpcChannel.ENTITY_READ_LANGUAGE>(code, Language, container.get(Service.LANGUAGE_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_SITE, (id) =>
read<IpcChannel.ENTITY_READ_SITE>(id, Site, container.get(Service.SITE_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_SITE_NAME, (id) =>
read<IpcChannel.ENTITY_READ_SITE_NAME>(id, SiteName, container.get(Service.SITE_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_SOURCE, (id) =>
read<IpcChannel.ENTITY_READ_SOURCE>(id, Source, container.get(Service.SOURCE_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_TAG, (id) =>
read<IpcChannel.ENTITY_READ_TAG>(id, Tag, container.get(Service.TAG_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_TAG_NAME, (id) =>
read<IpcChannel.ENTITY_READ_TAG_NAME>(id, TagName, container.get(Service.TAG_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_TRANSFORMATION, (id) =>
read<IpcChannel.ENTITY_READ_TRANSFORMATION>(id, Transformation, container.get(Service.TRANSFORMATION_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE, (id) =>
read<IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE>(
id,
TransformationType,
container.get(Service.TRANSFORMATION_TYPE_SERIALIZER),
),
);
ipcServer.answer(IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE_NAME, (id) =>
read<IpcChannel.ENTITY_READ_TRANSFORMATION_TYPE_NAME>(
id,
TransformationTypeName,
container.get(Service.TRANSFORMATION_TYPE_NAME_SERIALIZER),
),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK_AUTHOR, (id) =>
read<IpcChannel.ENTITY_READ_WORK_AUTHOR>(id, WorkAuthor, container.get(Service.WORK_AUTHOR_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK_CHARACTER, (id) =>
read<IpcChannel.ENTITY_READ_WORK_CHARACTER>(id, WorkCharacter, container.get(Service.WORK_CHARACTER_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK_CHARACTER_NAME, (id) =>
read<IpcChannel.ENTITY_READ_WORK_CHARACTER_NAME>(
id,
WorkCharacterName,
container.get(Service.WORK_CHARACTER_NAME_SERIALIZER),
),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK, (id) =>
read<IpcChannel.ENTITY_READ_WORK>(id, Work, container.get(Service.WORK_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK_NAME, (id) =>
read<IpcChannel.ENTITY_READ_WORK_NAME>(id, WorkName, container.get(Service.WORK_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORK_TAG, (id) =>
read<IpcChannel.ENTITY_READ_WORK_TAG>(id, WorkTag, container.get(Service.WORK_TAG_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORLD, (id) =>
read<IpcChannel.ENTITY_READ_WORLD>(id, World, container.get(Service.WORLD_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORLD_NAME, (id) =>
read<IpcChannel.ENTITY_READ_WORLD_NAME>(id, WorldName, container.get(Service.WORLD_NAME_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORLD_CHARACTER, (id) =>
read<IpcChannel.ENTITY_READ_WORLD_CHARACTER>(id, WorldCharacter, container.get(Service.WORLD_CHARACTER_SERIALIZER)),
);
ipcServer.answer(IpcChannel.ENTITY_READ_WORLD_CHARACTER_NAME, (id) =>
read<IpcChannel.ENTITY_READ_WORLD_CHARACTER_NAME>(
id,
WorldCharacterName,
container.get(Service.WORLD_CHARACTER_NAME_SERIALIZER),
),
);