load recipe by id
This commit is contained in:
parent
3638909761
commit
21742e3b24
5 changed files with 54 additions and 5 deletions
15
bruno/recipe-backend/getRecipeById.bru
Normal file
15
bruno/recipe-backend/getRecipeById.bru
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
meta {
|
||||
name: getRecipeById
|
||||
type: http
|
||||
seq: 6
|
||||
}
|
||||
|
||||
get {
|
||||
url: https://localhost:4000/recipe/
|
||||
body: none
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
}
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -33,4 +33,13 @@ router.post(
|
|||
})
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/:id",
|
||||
asyncHandler(async(req, res) => {
|
||||
const id = req.params.id;
|
||||
const responseDto = await recipeController.getRecipeById(id);
|
||||
res.status(201).json(responseDto);
|
||||
})
|
||||
)
|
||||
|
||||
export default router;
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { Repository, DeepPartial, ObjectLiteral } from "typeorm";
|
||||
import { Repository, DeepPartial } from "typeorm";
|
||||
import { AppDataSource } from "../data-source.js";
|
||||
import { AbstractEntity } from "../entities/AbstractEntity.js";
|
||||
|
||||
|
|
|
|||
|
|
@ -5,4 +5,21 @@ export class RecipeRepository extends AbstractRepository<RecipeEntity> {
|
|||
constructor() {
|
||||
super(RecipeEntity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find recipe including all relations by id
|
||||
* @param id Recipe id
|
||||
* @returns RecipeEntity including all relations such as ingredients groups, ingredients and instruction steps
|
||||
*/
|
||||
async findById(id: string): Promise<RecipeEntity | null> {
|
||||
return this.repo.findOne(
|
||||
{ where: { id } as any,
|
||||
relations: [
|
||||
'ingredientGroups',
|
||||
'ingredientGroups.ingredients',
|
||||
'instructionSteps'
|
||||
]
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue