Minor refactoring

This commit is contained in:
araemer 2026-02-22 12:39:59 +01:00
parent 801739ea30
commit 66da81baf8
3 changed files with 11 additions and 21 deletions

View file

@ -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!")
return this.updateRecipe(dto);
}
// merge changes into entity
this.mapper.mergeDtoIntoEntity(dto, recipeEntity);
// persist changes
savedEntity = await this.recipeRepository.update(recipeEntity);
}
return this.mapper.toDto(savedEntity);
}
/**
* Update recipe data

View file

@ -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<void> {
async delete(id: UUID|string|undefined): Promise<void> {
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`);

View file

@ -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<T extends AbstractEntity> {
this.repo = AppDataSource.getRepository(entity);
}
async findById(id: string): Promise<T | null> {
async findById(id: string|UUID): Promise<T | null> {
return this.repo.findOne({ where: { id } as any });
}