Minor refactoring
This commit is contained in:
parent
801739ea30
commit
66da81baf8
3 changed files with 11 additions and 21 deletions
|
|
@ -5,7 +5,6 @@ import { NotFoundError, ValidationError } from "../api/errors/httpErrors.js";
|
||||||
import { RecipeInstructionStepDto } from "../api/dtos/RecipeInstructionStepDto.js";
|
import { RecipeInstructionStepDto } from "../api/dtos/RecipeInstructionStepDto.js";
|
||||||
import { RecipeIngredientGroupDto } from "../api/dtos/RecipeIngredientGroupDto.js";
|
import { RecipeIngredientGroupDto } from "../api/dtos/RecipeIngredientGroupDto.js";
|
||||||
import { RecipeIngredientDto } from "../api/dtos/RecipeIngredientDto.js";
|
import { RecipeIngredientDto } from "../api/dtos/RecipeIngredientDto.js";
|
||||||
import { RecipeEntity } from "../entities/RecipeEntity.js";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controls all recipe specific actions
|
* Controls all recipe specific actions
|
||||||
|
|
@ -26,8 +25,7 @@ export class RecipeHandler {
|
||||||
if(recipeEntity === null){
|
if(recipeEntity === null){
|
||||||
throw new NotFoundError("recipe with id " + id + " not found!")
|
throw new NotFoundError("recipe with id " + id + " not found!")
|
||||||
}
|
}
|
||||||
const recipeDto = this.mapper.toDto(recipeEntity);
|
return this.mapper.toDto(recipeEntity);
|
||||||
return recipeDto;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -39,26 +37,12 @@ export class RecipeHandler {
|
||||||
if (!this.isRecipeDtoValid(dto)) {
|
if (!this.isRecipeDtoValid(dto)) {
|
||||||
throw new ValidationError("recipe data is not valid!")
|
throw new ValidationError("recipe data is not valid!")
|
||||||
}
|
}
|
||||||
var savedEntity: RecipeEntity;
|
|
||||||
const recipeId = dto.id
|
const recipeId = dto.id
|
||||||
if(recipeId === undefined || recipeId.length === 0){
|
if(recipeId === undefined || recipeId.length === 0){
|
||||||
// create new recipe
|
return this.createRecipe(dto);
|
||||||
const recipeEntity = this.mapper.toEntity(dto)
|
|
||||||
delete (recipeEntity as any).id;
|
|
||||||
savedEntity = await this.recipeRepository.create(recipeEntity);
|
|
||||||
} else {
|
} else {
|
||||||
// save existing Recipe
|
return this.updateRecipe(dto);
|
||||||
// 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.mapper.toDto(savedEntity);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Update recipe data
|
* Update recipe data
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
import { TagRepository } from "../repositories/TagRepository.js";
|
import { TagRepository } from "../repositories/TagRepository.js";
|
||||||
import { TagDtoEntityMapper } from "../mappers/TagDtoEntityMapper.js";
|
import { TagDtoEntityMapper } from "../mappers/TagDtoEntityMapper.js";
|
||||||
import { TagDto } from "../api/dtos/TagDto.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.
|
* Handles business logic for tag operations.
|
||||||
|
|
@ -63,7 +65,10 @@ export class TagHandler {
|
||||||
*
|
*
|
||||||
* @throws Error when the tag does not exist.
|
* @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);
|
const existing = await this.tagRepository.findById(id);
|
||||||
if (!existing) {
|
if (!existing) {
|
||||||
throw new Error(`Tag with id ${id} not found`);
|
throw new Error(`Tag with id ${id} not found`);
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
import { Repository, DeepPartial } from "typeorm";
|
import { Repository, DeepPartial } from "typeorm";
|
||||||
import { AppDataSource } from "../data-source.js";
|
import { AppDataSource } from "../data-source.js";
|
||||||
import { AbstractEntity } from "../entities/AbstractEntity.js";
|
import { AbstractEntity } from "../entities/AbstractEntity.js";
|
||||||
|
import {UUID} from "crypto";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Basic methods for saving, loading and deleting data
|
* Basic methods for saving, loading and deleting data
|
||||||
|
|
@ -12,7 +13,7 @@ export abstract class AbstractRepository<T extends AbstractEntity> {
|
||||||
this.repo = AppDataSource.getRepository(entity);
|
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 });
|
return this.repo.findOne({ where: { id } as any });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue