recipe-backend/src/mappers/RecipeDtoEntityMapper.ts
2025-09-28 12:24:59 +02:00

57 lines
No EOL
2.3 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
const instructionStepEntities = entity.instructionSteps;
const instructionStepDtos = instructionStepEntities.map((stepEntity) => this.instructionStepMapper.toDto(stepEntity));
dto.instructions = instructionStepDtos;
// map ingredient groups
const ingredientGroupEntities = entity.ingredientGroups;
const ingredientGroupDtos = ingredientGroupEntities.map((groupEntity) => this.ingredientGroupMapper.toDto(groupEntity));
dto.ingredientGroups = ingredientGroupDtos;
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
const instructionStepDtos = dto.instructions;
const instructionStepEntities = instructionStepDtos.map((stepDto) => this.instructionStepMapper.toEntity(stepDto));
entity.instructionSteps = instructionStepEntities;
// map ingredient groups
const ingredientGroupDtos = dto.ingredientGroups;
const ingredientGroupEntities = ingredientGroupDtos.map((ingredientGroupDto) => this.ingredientGroupMapper.toEntity(ingredientGroupDto));
entity.ingredientGroups = ingredientGroupEntities;
return entity;
}
}