Add and edit recipes (#3)

Co-authored-by: clfreville2 <clement.freville2@etu.uca.fr>
Reviewed-on: https://codefirst.iut.uca.fr/git/clement.freville2/tiramisu/pulls/3
This commit is contained in:
2024-06-17 14:10:13 +02:00
parent 91b8780d1d
commit 13059ca8d1
5 changed files with 116 additions and 6 deletions

View File

@@ -29,4 +29,20 @@ export class RecipeService {
get(id: number): Recipe | null {
return this.#recipes.find((recipe) => recipe.id === id) || null;
}
add(recipe: Omit<Recipe, 'id'>): void {
const id = this.#recipes.length ? Math.max(...this.#recipes.map((recipe) => recipe.id)) + 1 : 1;
this.#recipes.push({
id,
...recipe,
});
}
edit(recipe: Recipe): void {
for (let i = 0; i < this.#recipes.length; ++i) {
if (this.#recipes[i].id === recipe.id) {
this.#recipes[i] = recipe;
}
}
}
}