92 lines
No EOL
3.1 KiB
TypeScript
92 lines
No EOL
3.1 KiB
TypeScript
import { RecipeDto } from "../dtos/RecipeDto.js";
|
|
import { RecipeEntity } from "../entities/RecipeEntity.js";
|
|
import { AbstractDtoEntityMapper } from "./AbstractDtoEntityMapper.js";
|
|
import { RecipeIngredientGroupDtoEntityMapper } from "./RecipeIngredientGroupDtoEntityMapper.js";
|
|
import { RecipeInstructionStepDtoEntityMapper } from "./RecipeInstructionStepDtoEntityMapper.js";
|
|
|
|
export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<RecipeEntity,RecipeDto>{
|
|
constructor(
|
|
private instructionStepMapper : RecipeInstructionStepDtoEntityMapper,
|
|
private ingredientGroupMapper : RecipeIngredientGroupDtoEntityMapper
|
|
){
|
|
super();
|
|
}
|
|
|
|
toDto(entity: RecipeEntity): RecipeDto {
|
|
const dto = new RecipeDto();
|
|
this.mapBaseEntityToDto(entity, dto);
|
|
|
|
dto.title = entity.title;
|
|
dto.amount = entity.amount
|
|
dto.amountDescription = entity.amountDescription;
|
|
|
|
// map instructions
|
|
dto.instructions = entity.instructionSteps.map((stepEntity) => this.instructionStepMapper.toDto(stepEntity));
|
|
// @todo map ids dto.instructions.forEach(step => step.recipeId = entity.id); // set recipe relation explicitly!
|
|
|
|
|
|
// map ingredient groups
|
|
dto.ingredientGroups = entity.ingredientGroups.map((groupEntity) => this.ingredientGroupMapper.toDto(groupEntity));
|
|
|
|
return dto;
|
|
}
|
|
|
|
toEntity(dto: RecipeDto): RecipeEntity {
|
|
const entity = new RecipeEntity();
|
|
this.mapBaseDtoToEntity(dto, entity);
|
|
|
|
entity.title = dto.title;
|
|
entity.amount = dto.amount
|
|
entity.amountDescription = dto.amountDescription;
|
|
|
|
// map instructions
|
|
entity.instructionSteps = dto.instructions.map((stepDto) => {
|
|
const stepEntity = this.instructionStepMapper.toEntity(stepDto);
|
|
|
|
// Set the relation if the entity already exists in DB
|
|
if(entity.hasValidId()){
|
|
stepEntity.recipe = entity;
|
|
}
|
|
|
|
// If it's a new step (no id from client), let DB generate a new UUID
|
|
if (!stepDto.id) {
|
|
delete (stepEntity as any).id;
|
|
}
|
|
|
|
return stepEntity;
|
|
});
|
|
|
|
// map ingredient groups
|
|
entity.ingredientGroups = dto.ingredientGroups.map((groupDto) => {
|
|
const groupEntity = this.ingredientGroupMapper.toEntity(groupDto);
|
|
// Set the relation if the entity already exists in DB
|
|
if(entity.hasValidId()){
|
|
groupEntity.recipe = entity;
|
|
}
|
|
// If it's a new group (no id from client), let DB generate a new UUID
|
|
if (!groupDto.id) {
|
|
delete (groupEntity as any).id;
|
|
}
|
|
return groupEntity;
|
|
});
|
|
|
|
return entity;
|
|
}
|
|
|
|
mergeDtoIntoEntity(dto: RecipeDto, entity: RecipeEntity): RecipeEntity {
|
|
entity.title = dto.title;
|
|
entity.amount = dto.amount;
|
|
entity.amountDescription = dto.amountDescription;
|
|
|
|
// --- Instruction Steps ---
|
|
entity.instructionSteps = this.instructionStepMapper.mergeDtoListIntoEntityList(dto.instructions, entity.instructionSteps);
|
|
|
|
// --- Ingredient Groups ---
|
|
entity.ingredientGroups = this.ingredientGroupMapper.mergeDtoListIntoEntityList(dto.ingredientGroups, entity.ingredientGroups);
|
|
return entity
|
|
}
|
|
|
|
createNewEntity(): RecipeEntity {
|
|
return new RecipeEntity();
|
|
}
|
|
} |