diff options
author | Tucker Evans <tuckerevans24@gmail.com> | 2019-12-07 14:27:26 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-07 14:27:26 -0500 |
commit | 5cf3689290666f70cd631d7b3d40a2ed245a3fce (patch) | |
tree | e6f2938377890774ff3e80fae8f77797917a46e5 /recipeBuddy/src | |
parent | cafe1b4fd3cc02554f44ffbaa8467d867a6838cb (diff) | |
parent | 658dea27e47e3aab24598a74ee9940f82bea2368 (diff) |
Merge pull request #12 from tuckerevans/REST-service
Backend REST Service implementation
Diffstat (limited to 'recipeBuddy/src')
-rw-r--r-- | recipeBuddy/src/app/DataModels/ingredient.ts | 2 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/ingredients.spec.ts | 7 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/ingredients.ts | 29 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/recipe.ts | 12 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/step.ts | 2 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/steps.spec.ts | 7 | ||||
-rw-r--r-- | recipeBuddy/src/app/DataModels/steps.ts | 18 | ||||
-rw-r--r-- | recipeBuddy/src/app/REST_service/backend.service.spec.ts | 12 | ||||
-rw-r--r-- | recipeBuddy/src/app/REST_service/backend.service.ts | 93 | ||||
-rw-r--r-- | recipeBuddy/src/app/app.module.ts | 3 |
10 files changed, 116 insertions, 69 deletions
diff --git a/recipeBuddy/src/app/DataModels/ingredient.ts b/recipeBuddy/src/app/DataModels/ingredient.ts index 0ede1d1..c79b475 100644 --- a/recipeBuddy/src/app/DataModels/ingredient.ts +++ b/recipeBuddy/src/app/DataModels/ingredient.ts @@ -5,7 +5,7 @@ export class Ingredient { private type_: string; public constructor(name: string, amount: number, unit: string, type_: string) { - this.name = names; + this.name = name; this.amount = amount; this.unit = unit; this.type_ = type_; diff --git a/recipeBuddy/src/app/DataModels/ingredients.spec.ts b/recipeBuddy/src/app/DataModels/ingredients.spec.ts deleted file mode 100644 index 17b5858..0000000 --- a/recipeBuddy/src/app/DataModels/ingredients.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Ingredients } from './ingredients'; - -describe('Ingredients', () => { - it('should create an instance', () => { - expect(new Ingredients()).toBeTruthy(); - }); -}); diff --git a/recipeBuddy/src/app/DataModels/ingredients.ts b/recipeBuddy/src/app/DataModels/ingredients.ts deleted file mode 100644 index 6f96e69..0000000 --- a/recipeBuddy/src/app/DataModels/ingredients.ts +++ /dev/null @@ -1,29 +0,0 @@ -export class Ingredients { - private names: string[]; - private amounts: number[]; - private units: string[]; - private types: string[]; - - public constructor(names: string[], amounts: number[], units: string[], types: string[]) { - this.names = names; - this.amounts = amounts; - this.units = units; - this.types = types; - } - - public getNames(): string[] { - return this.names; - } - - public getAmounts(): number[] { - return this.amounts; - } - - public getUnits(): string[] { - return this.units; - } - - public getTypes(): string[] { - return this.types; - } -} diff --git a/recipeBuddy/src/app/DataModels/recipe.ts b/recipeBuddy/src/app/DataModels/recipe.ts index 3194adc..368f237 100644 --- a/recipeBuddy/src/app/DataModels/recipe.ts +++ b/recipeBuddy/src/app/DataModels/recipe.ts @@ -1,5 +1,5 @@ -import {Steps} from "./steps" -import {Ingredients} from "./ingredients" +import {Step} from "./step" +import {Ingredient} from "./ingredient" export class Recipe { private id: number; @@ -14,7 +14,7 @@ export class Recipe { private tags: string[]; private photos: string[]; - public constructor(id: number, name: string, description: string, ingredients: Ingredients, steps: Steps, servingSize: number, cookTime: number, rating: number, tags: string[]) { + public constructor(id: number, name: string, description: string, ingredients: Ingredient[], steps: Step[], servingSize: number, cookTime: number, rating: number, tags: string[]) { this.id = id; this.name = name; this.description = description; @@ -38,11 +38,11 @@ export class Recipe { return this.description; } - public getIngredients(): Ingredients { + public getIngredients(): Ingredient[] { return this.ingredients; } - public getSteps(): Steps { + public getSteps(): Step[] { return this.steps; } @@ -55,7 +55,7 @@ export class Recipe { } public getTimesCooked(): number { - return timesCooked; + return this.timesCooked; } public getRating(): number { return this.rating; diff --git a/recipeBuddy/src/app/DataModels/step.ts b/recipeBuddy/src/app/DataModels/step.ts index 674a6df..1c1ca7b 100644 --- a/recipeBuddy/src/app/DataModels/step.ts +++ b/recipeBuddy/src/app/DataModels/step.ts @@ -1,4 +1,4 @@ -export class Steps { +export class Step { private instruction: string; private timer: number; diff --git a/recipeBuddy/src/app/DataModels/steps.spec.ts b/recipeBuddy/src/app/DataModels/steps.spec.ts deleted file mode 100644 index e315565..0000000 --- a/recipeBuddy/src/app/DataModels/steps.spec.ts +++ /dev/null @@ -1,7 +0,0 @@ -import { Steps } from './steps'; - -describe('Steps', () => { - it('should create an instance', () => { - expect(new Steps()).toBeTruthy(); - }); -}); diff --git a/recipeBuddy/src/app/DataModels/steps.ts b/recipeBuddy/src/app/DataModels/steps.ts deleted file mode 100644 index 9061dc2..0000000 --- a/recipeBuddy/src/app/DataModels/steps.ts +++ /dev/null @@ -1,18 +0,0 @@ -export class Steps { - private instructions: string[]; - private timers: number[]; - - public contructor(instructions: string[], timers: number[]) { - this.instructions = instructions; - this.timers = timers; - } - - public getInstructions(): string[] { - return this.instructions; - } - - public getTimers(): number[] { - return this.timers; - } - -} diff --git a/recipeBuddy/src/app/REST_service/backend.service.spec.ts b/recipeBuddy/src/app/REST_service/backend.service.spec.ts new file mode 100644 index 0000000..2fe8217 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { BackendService } from './backend.service'; + +describe('BackendService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: BackendService = TestBed.get(BackendService); + expect(service).toBeTruthy(); + }); +}); diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts new file mode 100644 index 0000000..1a2d933 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -0,0 +1,93 @@ +import { Injectable } from '@angular/core' +import { HttpClient, HttpHeaders } from '@angular/common/http' +import { Observable, throwError } from 'rxjs' +import { retry, catchError, map } from 'rxjs/operators' +import { Recipe } from '../DataModels/recipe' + + +export interface Status { + Code: number; + Msg: string; +} +export interface MsgList { + Status: Status; + Data: number[]; +} + +export interface Msg { + Status: Status; + Data: Recipe; +} + + +@Injectable({ providedIn: 'root' }) + +/* BackendService class based on tutorial at: + * <https://www.positronx.io/angular-8-httpclient-http-tutorial-build-consume-restful-api/> + */ +export class BackendService { + apiURL = 'http://api.recipebuddy.xyz:8888' + + constructor( private http: HttpClient) + { + } + + httpOptions = {headers: new HttpHeaders( + {'Content-Type':'application/json'} + )} + + + getRecipes(): Observable<number[]> + { + return this.http.get<MsgList>(this.apiURL + '/recipes') + .pipe ( + retry(1), + map(msg => msg.Data), + catchError(this.handleError) + ) + } + + + getRecipe(id): Observable<Recipe> + { + return this.http.get<Msg>(this.apiURL + '/recipes/' + id) + .pipe ( + retry(1), + map(msg => msg.Data), + catchError(this.handleError) + ) + } + + createRecipe(data): Observable<Recipe> + { + return this.http.post<Recipe>(this.apiURL + '/recipes', + JSON.stringify(data), this.httpOptions) + } + + updateRecipe(data): Observable<Recipe> + { + return this.http.put<Recipe>(this.apiURL + '/recipes/' + data.id, + JSON.stringify(data), this.httpOptions) + } + + deleteRecipe(id): Observable<Msg> + { + return this.http.delete<Msg>(this.apiURL + '/recipes/' + id) + .pipe ( + retry(1), + catchError(this.handleError) + ) + } + + handleError(error) { + let errMsg = ''; + if (error.error instanceof ErrorEvent) { + errMsg = error.error.message; + } else { + errMsg = 'Error API'; + } + console.log(errMsg) + window.alert(errMsg) + return throwError(errMsg); + } +} diff --git a/recipeBuddy/src/app/app.module.ts b/recipeBuddy/src/app/app.module.ts index fd2059c..eaa0553 100644 --- a/recipeBuddy/src/app/app.module.ts +++ b/recipeBuddy/src/app/app.module.ts @@ -9,6 +9,8 @@ import { StepCardComponent } from './cook-page/step-card/step-card.component'; import { AppRoutingModule } from './app-routing.module'; +import {HttpClientModule } from '@angular/common/http' + @NgModule({ declarations: [ AppComponent, @@ -19,6 +21,7 @@ import { AppRoutingModule } from './app-routing.module'; BrowserModule, AppRoutingModule, MatCardModule, + HttpClientModule ], providers: [], bootstrap: [AppComponent] |