Fix bug when saving tags for recipes
This commit is contained in:
parent
66da81baf8
commit
96d87fed13
8 changed files with 81 additions and 17 deletions
|
|
@ -8,9 +8,9 @@ import {requireAdmin} from "../../middleware/authorizationMiddleware.js";
|
|||
* REST resource for tags.
|
||||
*
|
||||
* Routes:
|
||||
* GET /tags — list all tags (authenticated users)
|
||||
* POST /tags/create-or-update — create or update a tag (authenticated users)
|
||||
* DELETE /tags/:id — delete a tag (administrators only)
|
||||
* GET /tag — list all tags (authenticated users)
|
||||
* POST /tag/create-or-update — create or update a tag (authenticated users)
|
||||
* DELETE /tag/:id — delete a tag (administrators only)
|
||||
*
|
||||
* Authentication / authorisation is assumed to be enforced by middleware
|
||||
* applied upstream (e.g. a global JWT guard). The adminOnly middleware below
|
||||
|
|
@ -22,7 +22,7 @@ const tagHandler = new TagHandler();
|
|||
|
||||
|
||||
/**
|
||||
* GET /tags
|
||||
* GET /tag/all
|
||||
* Returns all available tags.
|
||||
*/
|
||||
router.get("/all", async (_req: Request, res: Response, next: NextFunction) => {
|
||||
|
|
@ -35,9 +35,9 @@ router.get("/all", async (_req: Request, res: Response, next: NextFunction) => {
|
|||
});
|
||||
|
||||
/**
|
||||
* POST /tags/create-or-update
|
||||
* POST /tag/create-or-update
|
||||
* Creates a new tag or updates an existing one.
|
||||
* If a tag with the given descriprion already exists in the database, that tag is returned instead of creating
|
||||
* If a tag with the given description already exists in the database, that tag is returned instead of creating
|
||||
* a duplicate.
|
||||
* Body: TagDto
|
||||
*/
|
||||
|
|
@ -55,7 +55,7 @@ router.post(
|
|||
);
|
||||
|
||||
/**
|
||||
* DELETE /tags/:id
|
||||
* DELETE /tag/:id
|
||||
* Deletes a tag by ID. Restricted to administrators.
|
||||
* The database cascade removes all entries in the recipe_tag mapping table.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import { AbstractDtoEntityMapper } from "./AbstractDtoEntityMapper.js";
|
|||
import { RecipeIngredientGroupDtoEntityMapper } from "./RecipeIngredientGroupDtoEntityMapper.js";
|
||||
import { RecipeInstructionStepDtoEntityMapper } from "./RecipeInstructionStepDtoEntityMapper.js";
|
||||
import { TagDtoEntityMapper } from "./TagDtoEntityMapper.js";
|
||||
import {TagEntity} from "../entities/TagEntity.js";
|
||||
|
||||
export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<RecipeEntity, RecipeDto> {
|
||||
constructor(
|
||||
|
|
@ -114,12 +115,15 @@ export class RecipeDtoEntityMapper extends AbstractDtoEntityMapper<RecipeEntity,
|
|||
);
|
||||
|
||||
// --- Tags ---
|
||||
// Tags are looked up by ID and replaced wholesale: the recipe holds
|
||||
// references to existing tag entities, so we map each DTO to its entity
|
||||
// form and let TypeORM sync the join table on save.
|
||||
entity.tagList = (dto.tagList ?? []).map((tagDto) =>
|
||||
this.tagMapper.toEntity(tagDto)
|
||||
);
|
||||
/* Only build a stub here instead of using the mapper to avoid causing a cascade on the tag table while
|
||||
* updating the join table correctly. For this purpose, the ORM only needs to know the tag ID in order to
|
||||
* signal that we are dealing with an existing tag.
|
||||
*/
|
||||
entity.tagList = (dto.tagList ?? []).map((tagDto) => {
|
||||
const stub = new TagEntity();
|
||||
stub.id = tagDto.id;
|
||||
return stub;
|
||||
});
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import { AbstractRepository } from "./AbstractRepository.js";
|
||||
import { RecipeEntity } from "../entities/RecipeEntity.js";
|
||||
import { AppDataSource } from "../data-source.js";
|
||||
import { ILike, Like } from "typeorm";
|
||||
import { ILike } from "typeorm";
|
||||
|
||||
export class RecipeRepository extends AbstractRepository<RecipeEntity> {
|
||||
constructor() {
|
||||
|
|
@ -19,7 +19,8 @@ export class RecipeRepository extends AbstractRepository<RecipeEntity> {
|
|||
relations: [
|
||||
'ingredientGroups',
|
||||
'ingredientGroups.ingredients',
|
||||
'instructionSteps'
|
||||
'instructionSteps',
|
||||
'tagList',
|
||||
]
|
||||
});
|
||||
}
|
||||
|
|
@ -46,7 +47,7 @@ export class RecipeRepository extends AbstractRepository<RecipeEntity> {
|
|||
// load existing data
|
||||
const existing = await this.repo.findOneOrFail({
|
||||
where: { id: entity.id },
|
||||
relations: ["instructionSteps", "ingredientGroups", "ingredientGroups.ingredients"],
|
||||
relations: ["instructionSteps", "ingredientGroups", "ingredientGroups.ingredients", "tagList"],
|
||||
});
|
||||
|
||||
// merge new entity and existing entity
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue