renaming and docs
This commit is contained in:
parent
b1b714f44e
commit
7e831cfb64
14 changed files with 86 additions and 32 deletions
|
|
@ -25,12 +25,16 @@ export abstract class AbstractDtoEntityMapper<
|
|||
return entity;
|
||||
}
|
||||
|
||||
// Abstract methods to be implemented by subclasses
|
||||
abstract toDto(entity: E): D;
|
||||
abstract toEntity(dto: D): E;
|
||||
abstract mergeDtoIntoEntity(dto: D, entity: E): E;
|
||||
abstract createNewEntity() : E;
|
||||
|
||||
/**
|
||||
* Merge entity list with changes contained in DTO list
|
||||
* @param dtos List of dtos
|
||||
* @param entities List of entities
|
||||
* @returns Merged list
|
||||
*
|
||||
* elements no longer contained in the dto list will be removed from the entity list
|
||||
* new elements will be mapped to entity and added to the entity list
|
||||
* existing elements will be updated
|
||||
*/
|
||||
mergeDtoListIntoEntityList(dtos: D[], entities: E[]) : E[]{
|
||||
const updatedEntities: E[] = [];
|
||||
const existingMap = new Map(entities?.map(e => [e.id, e]) ?? []);
|
||||
|
|
@ -49,4 +53,29 @@ export abstract class AbstractDtoEntityMapper<
|
|||
|
||||
return updatedEntities;
|
||||
}
|
||||
|
||||
// Abstract methods to be implemented by subclasses
|
||||
/**
|
||||
* Maps an entity to DTO
|
||||
* @param entity Entity that is mapped to DTO
|
||||
*/
|
||||
abstract toDto(entity: E): D;
|
||||
/**
|
||||
* Maps a DTO to entity
|
||||
* @param dto DTO to map to entity
|
||||
*/
|
||||
abstract toEntity(dto: D): E;
|
||||
/**
|
||||
* Merge changes in DTO into entity
|
||||
* @param dto Dto containing changes
|
||||
* @param entity existing entity
|
||||
*
|
||||
* Used for merging user changes (DTO) into the existing entity (database).
|
||||
*/
|
||||
abstract mergeDtoIntoEntity(dto: D, entity: E): E;
|
||||
/**
|
||||
* Defines how to create a new entity. Required by mergeDtoListIntoEntityList
|
||||
* to add new elements to the list
|
||||
*/
|
||||
abstract createNewEntity() : E;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,11 +17,11 @@ export class CompactRecipeDtoEntityMapper extends AbstractDtoEntityMapper<Recipe
|
|||
throw new Error("Mapping CompactRecipeDto to RecipeEntity is not allowed!");
|
||||
}
|
||||
|
||||
mergeDtoIntoEntity(dto: CompactRecipeDto, entity: RecipeEntity): RecipeEntity {
|
||||
createNewEntity() : RecipeEntity {
|
||||
throw new Error("Mapping CompactRecipeDto to RecipeEntity is not allowed!");
|
||||
}
|
||||
|
||||
createNewEntity() : RecipeEntity {
|
||||
mergeDtoIntoEntity(dto: CompactRecipeDto, entity: RecipeEntity): RecipeEntity {
|
||||
throw new Error("Mapping CompactRecipeDto to RecipeEntity is not allowed!");
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue