Skip to content

Commit 3a07998

Browse files
try-catch used
1 parent d330b6c commit 3a07998

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/infrastructure/database/repositories/recipe.repository-impl.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,44 @@ export class RecipeRepositoryImpl implements RecipeRepository {
3737
}
3838
}
3939
async findById(id: number): Promise<RecipeORMEntity | null> {
40-
const recipe = await this.Orm.findOne({ where: { id } });
41-
if (!recipe) {
40+
try {
41+
const recipe = await this.Orm.findOne({ where: { id } });
42+
if (!recipe) {
43+
throw new HttpException(
44+
i18nValidationMessage('validation.recipeNotFound'),
45+
HttpStatus.NOT_FOUND,
46+
);
47+
}
48+
return recipe;
49+
} catch (error) {
4250
throw new HttpException(
43-
i18nValidationMessage('validation.recipeNotFound'),
44-
HttpStatus.NOT_FOUND,
51+
i18nValidationMessage('validation.unableToFetchRecipe'),
52+
HttpStatus.INTERNAL_SERVER_ERROR,
4553
);
4654
}
47-
return recipe;
4855
}
4956
async update(recipe: RecipeORMEntity): Promise<RecipeORMEntity | null> {
50-
const existingRecipe = await this.Orm.findOne({ where: { id: recipe.id } });
51-
if (!existingRecipe) {
57+
try {
58+
const existingRecipe = await this.Orm.findOne({
59+
where: { id: recipe.id },
60+
});
61+
if (!existingRecipe) {
62+
throw new HttpException(
63+
i18nValidationMessage('validation.recipeNotFound'),
64+
HttpStatus.NOT_FOUND,
65+
);
66+
}
67+
existingRecipe.title = recipe.title;
68+
existingRecipe.instructions = recipe.instructions;
69+
existingRecipe.ingredients = recipe.ingredients;
70+
existingRecipe.updatedAt = new Date();
71+
await this.Orm.save(existingRecipe);
72+
return existingRecipe;
73+
} catch (error) {
5274
throw new HttpException(
53-
i18nValidationMessage('validation.recipeNotFound'),
54-
HttpStatus.NOT_FOUND,
75+
i18nValidationMessage('validation.unableToUpdateRecipe'),
76+
HttpStatus.INTERNAL_SERVER_ERROR,
5577
);
5678
}
57-
existingRecipe.title = recipe.title;
58-
existingRecipe.instructions = recipe.instructions;
59-
existingRecipe.ingredients = recipe.ingredients;
60-
existingRecipe.updatedAt = new Date();
61-
await this.Orm.save(existingRecipe);
62-
return existingRecipe;
6379
}
6480
}

0 commit comments

Comments
 (0)