Skip to content

Commit

Permalink
image deletion fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzlars committed Jan 5, 2025
1 parent d827072 commit c333460
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/recipes/recipes.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,17 @@ export class RecipesService {
await queryRunner.startTransaction();

try {
const { image, ...recipe } = await queryRunner.manager.findOne(
RecipeEntity,
{
where: { id },
relations: ['image'],
},
);
const recipe = await queryRunner.manager.findOne(RecipeEntity, {
where: { id },
relations: ['image'],
});

if (!recipe) {
throw new NotFoundException();
}

const imageId = recipe.image?.id;
// TODO: Can typeorm delete the related entries at once?
const deleteRecipeResult = await queryRunner.manager.delete(
RecipeEntity,
{ id: id },
Expand All @@ -140,7 +139,9 @@ export class RecipesService {
throw new NotFoundException();
}

await this.imagesService.removeImage(image.id, queryRunner.manager);
if (imageId) {
await this.imagesService.removeImage(imageId, queryRunner.manager);
}

await queryRunner.commitTransaction();
} catch (error) {
Expand Down Expand Up @@ -253,12 +254,14 @@ export class RecipesService {
throw new NotFoundException();
}

const imageId = recipe.image.id;
const imageId = recipe.image?.id;
recipe.image = null;

await queryRunner.manager.save(recipe);

await this.imagesService.removeImage(imageId, queryRunner.manager);
if (imageId) {
await this.imagesService.removeImage(imageId, queryRunner.manager);
}

await queryRunner.commitTransaction();
} catch (error) {
Expand Down

0 comments on commit c333460

Please sign in to comment.