diff --git a/src/handlers/RecipeHandler.ts b/src/handlers/RecipeHandler.ts index e90e951..99172eb 100644 --- a/src/handlers/RecipeHandler.ts +++ b/src/handlers/RecipeHandler.ts @@ -5,7 +5,6 @@ import { NotFoundError, ValidationError } from "../api/errors/httpErrors.js"; import { RecipeInstructionStepDto } from "../api/dtos/RecipeInstructionStepDto.js"; import { RecipeIngredientGroupDto } from "../api/dtos/RecipeIngredientGroupDto.js"; import { RecipeIngredientDto } from "../api/dtos/RecipeIngredientDto.js"; -import { RecipeEntity } from "../entities/RecipeEntity.js"; /** * Controls all recipe specific actions @@ -26,8 +25,7 @@ export class RecipeHandler { if(recipeEntity === null){ throw new NotFoundError("recipe with id " + id + " not found!") } - const recipeDto = this.mapper.toDto(recipeEntity); - return recipeDto; + return this.mapper.toDto(recipeEntity); } /** @@ -39,26 +37,12 @@ export class RecipeHandler { if (!this.isRecipeDtoValid(dto)) { throw new ValidationError("recipe data is not valid!") } - var savedEntity: RecipeEntity; const recipeId = dto.id 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); + return this.createRecipe(dto); } else { - // save existing Recipe - // First: Load current version of recipe from database - const recipeEntity = await this.recipeRepository.findById(recipeId); - if(!recipeEntity){ - throw new ValidationError("No recipe with ID " + recipeId + " found in database!") - } - // merge changes into entity - this.mapper.mergeDtoIntoEntity(dto, recipeEntity); - // persist changes - savedEntity = await this.recipeRepository.update(recipeEntity); + return this.updateRecipe(dto); } - return this.mapper.toDto(savedEntity); } /** * Update recipe data diff --git a/src/handlers/TagHandler.ts b/src/handlers/TagHandler.ts index 1bcaca2..2c4a72e 100644 --- a/src/handlers/TagHandler.ts +++ b/src/handlers/TagHandler.ts @@ -1,6 +1,8 @@ import { TagRepository } from "../repositories/TagRepository.js"; import { TagDtoEntityMapper } from "../mappers/TagDtoEntityMapper.js"; import { TagDto } from "../api/dtos/TagDto.js"; +import {UUID} from "crypto"; +import {ValidationError} from "../api/errors/httpErrors.js"; /** * Handles business logic for tag operations. @@ -63,7 +65,10 @@ export class TagHandler { * * @throws Error when the tag does not exist. */ - async delete(id: string): Promise { + async delete(id: UUID|string|undefined): Promise { + if(!id){ + throw new ValidationError("id is required for deleting tag"); + } const existing = await this.tagRepository.findById(id); if (!existing) { throw new Error(`Tag with id ${id} not found`); diff --git a/src/repositories/AbstractRepository.ts b/src/repositories/AbstractRepository.ts index 1404e0a..632c952 100644 --- a/src/repositories/AbstractRepository.ts +++ b/src/repositories/AbstractRepository.ts @@ -1,6 +1,7 @@ import { Repository, DeepPartial } from "typeorm"; import { AppDataSource } from "../data-source.js"; import { AbstractEntity } from "../entities/AbstractEntity.js"; +import {UUID} from "crypto"; /** * Basic methods for saving, loading and deleting data @@ -12,7 +13,7 @@ export abstract class AbstractRepository { this.repo = AppDataSource.getRepository(entity); } - async findById(id: string): Promise { + async findById(id: string|UUID): Promise { return this.repo.findOne({ where: { id } as any }); }