save recipes

This commit is contained in:
Anika Raemer 2025-09-28 12:24:59 +02:00
parent 3a887d8dbb
commit 380eb4cd21
18 changed files with 412 additions and 87 deletions

View file

@ -1,17 +1,34 @@
import { RecipeDto } from "../dtos/RecipeDto.js";
import { RecipeEntity } from "../entities/RecipeEntity.js";
import { ValidationError } from "../errors/httpErrors.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 = dto.amountDescription;
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;
}
@ -20,11 +37,20 @@ export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<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;
}

View file

@ -0,0 +1,32 @@
import { RecipeIngredientDto } from "../dtos/RecipeIngredientDto.js";
import { RecipeIngredientEntity } from "../entities/RecipeIngredientEntity.js";
import { AbstractDtoEntityMapper } from "./AbstractDtoEntityMapper.js";
export class RecipeIngredientDtoEntityMapper extends AbstractDtoEntityMapper<RecipeIngredientEntity, RecipeIngredientDto>{
toDto(entity: RecipeIngredientEntity): RecipeIngredientDto {
const dto = new RecipeIngredientDto();
this.mapBaseEntityToDto(entity, dto);
dto.amount = entity.amount;
dto.name = entity.name;
dto.unit = entity.unit;
dto.sortOrder = entity.sortOrder;
dto.subtext = entity.subtext;
return dto;
}
toEntity(dto: RecipeIngredientDto): RecipeIngredientEntity {
const entity = new RecipeIngredientEntity();
this.mapBaseDtoToEntity(dto,entity);
entity.amount = dto.amount;
entity.name = dto.name;
entity.unit = dto.unit;
entity.sortOrder = dto.sortOrder;
entity.subtext = dto.subtext;
return entity;
}
}

View file

@ -0,0 +1,43 @@
import { RecipeIngredientGroupDto } from "../dtos/RecipeIngredientGroupDto.js";
import { RecipeIngredientGroupEntity } from "../entities/RecipeIngredientGroupEntity.js";
import { AbstractDtoEntityMapper } from "./AbstractDtoEntityMapper.js";
import { RecipeIngredientDtoEntityMapper } from "./RecipeIngredientDtoEntityMapper.js";
export class RecipeIngredientGroupDtoEntityMapper extends AbstractDtoEntityMapper<RecipeIngredientGroupEntity,RecipeIngredientGroupDto>{
constructor(
private ingredientMapper : RecipeIngredientDtoEntityMapper
){
super();
}
toDto(entity: RecipeIngredientGroupEntity): RecipeIngredientGroupDto {
const dto = new RecipeIngredientGroupDto();
this.mapBaseEntityToDto(entity, dto);
dto.title = entity.title;
dto.sortOrder = entity.sortOrder
// map ingredients
const ingredientEntities = entity.ingredients;
const ingredientDtos = ingredientEntities?.map((ingredientEntity) => this.ingredientMapper.toDto(ingredientEntity));
dto.ingredients = ingredientDtos;
return dto;
}
toEntity(dto: RecipeIngredientGroupDto): RecipeIngredientGroupEntity {
const entity = new RecipeIngredientGroupEntity();
this.mapBaseDtoToEntity(dto, entity);
entity.title = dto.title;
entity.sortOrder = dto.sortOrder
// map ingredients
const ingredientDtos = dto.ingredients;
const ingredientEntities = ingredientDtos?.map((ingredientDto) => this.ingredientMapper.toEntity(ingredientDto));
entity.ingredients = ingredientEntities;
return entity;
}
}

View file

@ -0,0 +1,27 @@
import { RecipeInstructionStepDto } from "../dtos/RecipeInstructionStepDto.js";
import { RecipeInstructionStepEntity } from "../entities/RecipeInstructionStepEntity.js";
import { AbstractDtoEntityMapper } from "./AbstractDtoEntityMapper.js";
export class RecipeInstructionStepDtoEntityMapper extends AbstractDtoEntityMapper<RecipeInstructionStepEntity,RecipeInstructionStepDto>{
toDto(entity: RecipeInstructionStepEntity): RecipeInstructionStepDto {
const dto = new RecipeInstructionStepDto();
this.mapBaseEntityToDto(entity, dto);
dto.text = entity.text;
dto.sortOrder = entity.sortOrder
return dto;
}
toEntity(dto: RecipeInstructionStepDto): RecipeInstructionStepEntity {
const entity = new RecipeInstructionStepEntity();
this.mapBaseDtoToEntity(dto, entity);
entity.text = dto.text;
entity.sortOrder = dto.sortOrder
return entity;
}
}