fix relations

This commit is contained in:
Anika Raemer 2025-10-10 19:41:34 +02:00
parent 58ef7fbc00
commit 0587153829
8 changed files with 64 additions and 79 deletions

View file

@ -7,7 +7,6 @@ import { RecipeDto } from "../dtos/RecipeDto.js";
import { RecipeIngredientDtoEntityMapper } from "../mappers/RecipeIngredientDtoEntityMapper.js";
import { RecipeIngredientGroupDtoEntityMapper } from "../mappers/RecipeIngredientGroupDtoEntityMapper.js";
import { RecipeInstructionStepDtoEntityMapper } from "../mappers/RecipeInstructionStepDtoEntityMapper.js";
import { ValidationError } from "../errors/httpErrors.js";
/**
* Handles all recipe related routes
@ -22,20 +21,6 @@ const recipeInstructionStepMapper = new RecipeInstructionStepDtoEntityMapper();
const recipeMapper = new RecipeDtoEntityMapper(recipeInstructionStepMapper, recipeIngredientGroupMapper);
const recipeController = new RecipeHandler(recipeRepository, recipeMapper);
/**
* Create new recipe
* Consumes: RecipeDto
* Responds with RecipeDto
* DEPRECATED!
*/
router.post(
"/",
asyncHandler(async (req, res) => {
const requestDto : RecipeDto = req.body;
const responseDto = await recipeController.createRecipe(requestDto);
res.status(201).json(responseDto);
})
);
/**
* Save or update recipe.
@ -67,24 +52,4 @@ router.get(
})
);
/**
* Saves existing recipe
* Also handles changes to instructions steps and ingredient (groups)
* Consumes: RecipeDto
* Responds with RecipeDto
* DEPRECATED
*/
router.put(
"/:id",
asyncHandler(async(req, res) =>{
const id = req.params.id;
const recipeDto : RecipeDto = req.body;
if(id != recipeDto.id){
throw new ValidationError("Cannot save recipe! ID in request body " + recipeDto.id + " doesn't match ID in path " + id +"!")
}
const responseDto = await recipeController.updateRecipe(recipeDto);
res.status(201).json(responseDto);
})
);
export default router;

View file

@ -16,4 +16,12 @@ export abstract class AbstractEntity {
@UpdateDateColumn({name: "update_date"})
updateDate?: Date;
/**
* Checks whether entity has a valid ID
* @todo check for valid UUID...
*/
hasValidId() : boolean {
return this.id !== undefined && this.id.length > 0;
}
}

View file

@ -41,11 +41,11 @@ export class RecipeHandler {
}
var savedEntity: RecipeEntity;
const recipeId = dto.id
if(recipeId === undefined){
if(recipeId === undefined || recipeId.length === 0){
// create new recipe
const recipeEntity = this.mapper.toEntity(dto)
delete (recipeEntity as any).id;
savedEntity = await this.recipeRepository.create(recipeEntity);
throw new ValidationError("Trying to update recipe without ID!")
} else {
// save existing Recipe
// First: Load current version of recipe from database

View file

@ -43,8 +43,10 @@ export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<RecipeEntity,
entity.instructionSteps = dto.instructions.map((stepDto) => {
const stepEntity = this.instructionStepMapper.toEntity(stepDto);
// Always set the relation
stepEntity.recipe = entity;
// 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) {
@ -57,7 +59,11 @@ export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<RecipeEntity,
// map ingredient groups
entity.ingredientGroups = dto.ingredientGroups.map((groupDto) => {
const groupEntity = this.ingredientGroupMapper.toEntity(groupDto);
groupEntity.recipe = entity;
// 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;
}

View file

@ -33,7 +33,10 @@ export class RecipeIngredientGroupDtoEntityMapper extends AbstractDtoEntityMappe
// map ingredients
entity.ingredients = dto.ingredients.map((ingredientDto) => {
const ingredientEntity = this.ingredientMapper.toEntity(ingredientDto);
ingredientEntity.ingredientGroup = entity;
// set relation if entity already exists in DB
if(entity.hasValidId()){
ingredientEntity.ingredientGroup = entity;
}
// remove id from new entity completely and allow ORM to generate a new one
if (!ingredientDto.id) {
delete (ingredientEntity as any).id;

View file

@ -8,8 +8,9 @@ import { Request, Response, NextFunction } from "express";
*/
export function corsHeaders (req: Request, res: Response, next: NextFunction) {
// allow requests from all sources (*) for now
// @todo restrict access!
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.header('Access-Control-Allow-Methods', 'GET,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests quickly