load recipe by id

This commit is contained in:
Anika Raemer 2025-09-28 20:14:19 +02:00
parent 3638909761
commit 21742e3b24
5 changed files with 54 additions and 5 deletions

View file

@ -1,7 +1,7 @@
import { RecipeDto } from "../dtos/RecipeDto.js";
import { RecipeDtoEntityMapper } from "../mappers/RecipeDtoEntityMapper.js";
import { RecipeRepository } from "../repositories/RecipeRepository.js";
import { ValidationError } from "../errors/httpErrors.js";
import { NotFoundError, ValidationError } from "../errors/httpErrors.js";
import { RecipeInstructionStepDto } from "../dtos/RecipeInstructionStepDto.js";
import { RecipeIngredientGroupDto } from "../dtos/RecipeIngredientGroupDto.js";
import { RecipeIngredientDto } from "../dtos/RecipeIngredientDto.js";
@ -11,10 +11,18 @@ import { RecipeIngredientDto } from "../dtos/RecipeIngredientDto.js";
*/
export class RecipeController {
constructor(
private repository: RecipeRepository,
private recipeRepository: RecipeRepository,
private mapper: RecipeDtoEntityMapper
) { }
async getRecipeById(id: string){
const recipeEntity = await this.recipeRepository.findById(id);
if(recipeEntity === null){
throw new NotFoundError("recipe with id " + id + " not found!")
}
const recipeDto = this.mapper.toDto(recipeEntity);
return recipeDto;
}
/**
* Create a new recipe
* @param dto RecipeDto containing the new recipe
@ -24,8 +32,8 @@ export class RecipeController {
throw new ValidationError("recipe data is not valid!")
}
const recipeEntity = this.mapper.toEntity(dto)
const entity = await this.repository.create(recipeEntity);
return this.mapper.toDto(entity);
const savedEntity = await this.recipeRepository.create(recipeEntity);
return this.mapper.toDto(savedEntity);
}
/**