RenaiApp/src/renderer/store/repositories/multi-named-entity-reposito...

71 lines
2.5 KiB
TypeScript

import {
EditableEntityRepository,
EditableEntityRepositoryInterface,
RelatedRepositoryUpdaters,
UpdatePartialFillers,
} from './editable-entity-repository';
export interface MultiNamedEntityRepositoryInterface<
Serialized extends MultiNamedInterface,
NameSerialized extends NameSerializedInterface,
> extends EditableEntityRepositoryInterface<Serialized> {
/**
* adds a name to a multi-named entity
*
* indirectly calls the subscribers of this entity by creating the name
* via the name repository which updates this related repository
*
* @param id - the ID of the multi-named entity
* @param name - the name to add
*/
addName(id: number, name: string): Promise<NameSerialized>;
/**
* removes a name from a multi-named entity
*
* indirectly calls the subscribers of this entity by deleting the name
* via the name repository which updates this related repository
*
* @param id - the ID of the multi-named entity
* @param nameId - the ID of the name to delete
*/
removeName(id: number, nameId: number): Promise<void>;
}
export class MultiNamedEntityRepository<
Serialized extends MultiNamedInterface,
NameSerialized extends NameSerializedInterface,
>
extends EditableEntityRepository<Serialized>
implements MultiNamedEntityRepositoryInterface<Serialized, NameSerialized>
{
private readonly nameRepositoryPromise: Promise<EditableEntityRepositoryInterface<NameSerialized>>;
public constructor(
apiRead: (identifier: number) => Promise<Serialized>,
apiCreate: (partial: Partial<Serialized>) => Promise<Serialized>,
apiUpdate: (identifier: number, partial: Partial<Serialized>) => Promise<Serialized>,
apiDelete: (identifier: number) => Promise<void>,
updatePartialFillers: UpdatePartialFillers<Serialized>,
relatedRepositoryUpdaters: RelatedRepositoryUpdaters<Serialized>,
nameRepositoryPromise: Promise<EditableEntityRepositoryInterface<NameSerialized>>,
) {
super(apiRead, apiCreate, apiUpdate, apiDelete, updatePartialFillers, relatedRepositoryUpdaters);
this.nameRepositoryPromise = nameRepositoryPromise;
}
public async addName(id: number, name: string): Promise<NameSerialized> {
const nameRepository = await this.nameRepositoryPromise;
// @ts-ignore - yoo, help
return nameRepository.create({
name: name,
entity: id,
});
}
public async removeName(id: number, nameId: number): Promise<void> {
const nameRepository = await this.nameRepositoryPromise;
return nameRepository.delete(nameId);
}
}