From 4b5b05f396cc9056f6a11a90c5bc7ea4c237e7f8 Mon Sep 17 00:00:00 2001 From: clfreville2 Date: Fri, 14 Jun 2024 14:38:41 +0200 Subject: [PATCH] Add RecipeService --- .gitignore | 1 + src/app/app.component.ts | 2 +- src/app/app.routes.ts | 12 ++++++------ .../recipe-add/recipe-add.component.spec.ts | 4 ++-- src/app/recipe-add/recipe-add.component.ts | 1 - src/app/recipe.service.spec.ts | 16 ++++++++++++++++ src/app/recipe.service.ts | 19 +++++++++++++++++++ src/app/recipe/recipe.component.spec.ts | 4 ++-- src/app/recipe/recipe.component.ts | 4 ---- src/app/recipes/recipes.component.spec.ts | 4 ++-- src/app/recipes/recipes.component.ts | 1 - src/cookbook/type.ts | 6 +++--- 12 files changed, 52 insertions(+), 22 deletions(-) create mode 100644 src/app/recipe.service.spec.ts create mode 100644 src/app/recipe.service.ts diff --git a/.gitignore b/.gitignore index cc7b141..dfbb00a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ /tmp /out-tsc /bazel-out +target # Node /node_modules diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 86c087d..59cc757 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { RouterOutlet, RouterLink, RouterLinkActive } from '@angular/router'; +import { RouterLink, RouterLinkActive, RouterOutlet } from '@angular/router'; @Component({ selector: 'app-root', diff --git a/src/app/app.routes.ts b/src/app/app.routes.ts index 6632ce5..be6840b 100644 --- a/src/app/app.routes.ts +++ b/src/app/app.routes.ts @@ -1,10 +1,10 @@ import { Routes } from '@angular/router'; -import {RecipesComponent} from './recipes/recipes.component' -import {RecipeComponent} from './recipe/recipe.component' -import {RecipeAddComponent} from './recipe-add/recipe-add.component' +import { RecipeAddComponent } from './recipe-add/recipe-add.component'; +import { RecipeComponent } from './recipe/recipe.component'; +import { RecipesComponent } from './recipes/recipes.component'; export const routes: Routes = [ - { path: 'recipes', component: RecipesComponent}, - { path: 'recipe/add', component: RecipeAddComponent}, - { path: 'recipe/:id', component: RecipeComponent} + { path: 'recipes', component: RecipesComponent }, + { path: 'recipe/add', component: RecipeAddComponent }, + { path: 'recipe/:id', component: RecipeComponent }, ]; diff --git a/src/app/recipe-add/recipe-add.component.spec.ts b/src/app/recipe-add/recipe-add.component.spec.ts index fdc48f0..bcc1728 100644 --- a/src/app/recipe-add/recipe-add.component.spec.ts +++ b/src/app/recipe-add/recipe-add.component.spec.ts @@ -8,9 +8,9 @@ describe('RecipeAddComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [RecipeAddComponent] + imports: [RecipeAddComponent], }) - .compileComponents(); + .compileComponents(); fixture = TestBed.createComponent(RecipeAddComponent); component = fixture.componentInstance; diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts index a68ebba..0ca445c 100644 --- a/src/app/recipe-add/recipe-add.component.ts +++ b/src/app/recipe-add/recipe-add.component.ts @@ -7,5 +7,4 @@ import { Component } from '@angular/core'; templateUrl: './recipe-add.component.html', }) export class RecipeAddComponent { - } diff --git a/src/app/recipe.service.spec.ts b/src/app/recipe.service.spec.ts new file mode 100644 index 0000000..50345f6 --- /dev/null +++ b/src/app/recipe.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { RecipeService } from './recipe.service'; + +describe('RecipeService', () => { + let service: RecipeService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(RecipeService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/recipe.service.ts b/src/app/recipe.service.ts new file mode 100644 index 0000000..ef81924 --- /dev/null +++ b/src/app/recipe.service.ts @@ -0,0 +1,19 @@ +import { Injectable } from '@angular/core'; +import { Recipe } from '../cookbook/type'; + +@Injectable({ + providedIn: 'root', +}) +export class RecipeService { + #recipes: Recipe[] = []; + + constructor() {} + + getAll(): Promise { + return Promise.resolve(this.#recipes); + } + + get(id: number): Recipe | null { + return this.#recipes.find((recipe) => recipe.id === id) || null; + } +} diff --git a/src/app/recipe/recipe.component.spec.ts b/src/app/recipe/recipe.component.spec.ts index 92bf4db..30ef71b 100644 --- a/src/app/recipe/recipe.component.spec.ts +++ b/src/app/recipe/recipe.component.spec.ts @@ -8,9 +8,9 @@ describe('RecipeComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [RecipeComponent] + imports: [RecipeComponent], }) - .compileComponents(); + .compileComponents(); fixture = TestBed.createComponent(RecipeComponent); component = fixture.componentInstance; diff --git a/src/app/recipe/recipe.component.ts b/src/app/recipe/recipe.component.ts index d3b1822..a34cb5a 100644 --- a/src/app/recipe/recipe.component.ts +++ b/src/app/recipe/recipe.component.ts @@ -7,11 +7,7 @@ import { Component, Input } from '@angular/core'; templateUrl: './recipe.component.html', }) export class RecipeComponent { - @Input() set id(id: string) { - } - - } diff --git a/src/app/recipes/recipes.component.spec.ts b/src/app/recipes/recipes.component.spec.ts index 1d081f6..bd48d45 100644 --- a/src/app/recipes/recipes.component.spec.ts +++ b/src/app/recipes/recipes.component.spec.ts @@ -8,9 +8,9 @@ describe('RecipesComponent', () => { beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [RecipesComponent] + imports: [RecipesComponent], }) - .compileComponents(); + .compileComponents(); fixture = TestBed.createComponent(RecipesComponent); component = fixture.componentInstance; diff --git a/src/app/recipes/recipes.component.ts b/src/app/recipes/recipes.component.ts index ff4f2ac..3cc5d83 100644 --- a/src/app/recipes/recipes.component.ts +++ b/src/app/recipes/recipes.component.ts @@ -7,5 +7,4 @@ import { Component } from '@angular/core'; templateUrl: './recipes.component.html', }) export class RecipesComponent { - } diff --git a/src/cookbook/type.ts b/src/cookbook/type.ts index 04c1c06..7e12ef2 100644 --- a/src/cookbook/type.ts +++ b/src/cookbook/type.ts @@ -1,9 +1,9 @@ -type Ingredient = { +export type Ingredient = { id: number; name: string; }; -type Recipe = { +export type Recipe = { id: number; name: string; description: string; @@ -11,7 +11,7 @@ type Recipe = { ingredients: IngredientEntry[]; }; -type IngredientEntry = { +export type IngredientEntry = { idIngredient: number; idRecipe: number; quantity: number;