From 3970d90fc06a0cc55c260a1155202eaba18e373b Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 27 Nov 2019 22:40:42 -0500 Subject: Add base code for BackendService --- .../src/app/REST_service/backend.service.spec.ts | 12 +++++ .../src/app/REST_service/backend.service.ts | 51 ++++++++++++++++++++++ recipeBuddy/src/app/app.module.ts | 3 ++ 3 files changed, 66 insertions(+) create mode 100644 recipeBuddy/src/app/REST_service/backend.service.spec.ts create mode 100644 recipeBuddy/src/app/REST_service/backend.service.ts 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..20e8bf9 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { BackendServiceService } from './backend-service.service'; + +describe('BackendServiceService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: BackendServiceService = TestBed.get(BackendServiceService); + 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..7834cb5 --- /dev/null +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -0,0 +1,51 @@ +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: + * + */ +export class BackendService { + apiURL = 'http://api.recipebuddy.xyz:8888' + + constructor( private http: HttpClient) + { + } + + httpOptions = {headers: new HttpHeaders( + {'Content-Type':'application/json'} + )} + + + 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] -- cgit v1.1 From b58be7e07676839d196eb3090b651448b1380249 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 27 Nov 2019 22:41:40 -0500 Subject: Add getRecipes to BackendService --- recipeBuddy/src/app/REST_service/backend.service.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts index 7834cb5..cc08bc6 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -37,6 +37,16 @@ export class BackendService { )} + getRecipes(): Observable + { + return this.http.get(this.apiURL + '/recipes') + .pipe ( + retry(1), + map(msg => msg.Data), + catchError(this.handleError) + ) + } + handleError(error) { let errMsg = ''; if (error.error instanceof ErrorEvent) { -- cgit v1.1 From daf0ba705ff8692e83e8343778bc34cbc93beff3 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Wed, 27 Nov 2019 23:12:01 -0500 Subject: Fix renamed BackendServiceService BackendService was created as BackendServiceService. This was renamed in all other files but not in backend.service.spec.ts. --- recipeBuddy/src/app/REST_service/backend.service.spec.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/recipeBuddy/src/app/REST_service/backend.service.spec.ts b/recipeBuddy/src/app/REST_service/backend.service.spec.ts index 20e8bf9..2fe8217 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.spec.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.spec.ts @@ -1,12 +1,12 @@ import { TestBed } from '@angular/core/testing'; -import { BackendServiceService } from './backend-service.service'; +import { BackendService } from './backend.service'; -describe('BackendServiceService', () => { +describe('BackendService', () => { beforeEach(() => TestBed.configureTestingModule({})); it('should be created', () => { - const service: BackendServiceService = TestBed.get(BackendServiceService); + const service: BackendService = TestBed.get(BackendService); expect(service).toBeTruthy(); }); }); -- cgit v1.1 From f94d81c5283cbf5be2f2a96faa21807e381e1c5c Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 19:48:15 -0500 Subject: Fix typos in DataModels Mostly variable name fixes and type fixes. Also delete unused DataModels --- recipeBuddy/src/app/DataModels/ingredient.ts | 2 +- recipeBuddy/src/app/DataModels/ingredients.spec.ts | 7 ------ recipeBuddy/src/app/DataModels/ingredients.ts | 29 ---------------------- recipeBuddy/src/app/DataModels/recipe.ts | 12 ++++----- recipeBuddy/src/app/DataModels/step.ts | 2 +- recipeBuddy/src/app/DataModels/steps.spec.ts | 7 ------ recipeBuddy/src/app/DataModels/steps.ts | 18 -------------- 7 files changed, 8 insertions(+), 69 deletions(-) delete mode 100644 recipeBuddy/src/app/DataModels/ingredients.spec.ts delete mode 100644 recipeBuddy/src/app/DataModels/ingredients.ts delete mode 100644 recipeBuddy/src/app/DataModels/steps.spec.ts delete mode 100644 recipeBuddy/src/app/DataModels/steps.ts 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; - } - -} -- cgit v1.1 From 05da6e948976810317049e981fc0d0535669b482 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 20:09:37 -0500 Subject: Update angular dependencies --- recipeBuddy/package-lock.json | 1 + recipeBuddy/package.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/recipeBuddy/package-lock.json b/recipeBuddy/package-lock.json index dcb6c6d..0eca743 100644 --- a/recipeBuddy/package-lock.json +++ b/recipeBuddy/package-lock.json @@ -1057,6 +1057,7 @@ "version": "8.2.3", "resolved": "https://registry.npmjs.org/@angular/material/-/material-8.2.3.tgz", "integrity": "sha512-SOczkIaqes+r+9XF/UUiokidfFKBpHkOPIaFK857sFD0FBNPvPEpOr5oHKCG3feERRwAFqHS7Wo2ohVEWypb5A==", + "dev": true, "requires": { "tslib": "^1.7.1" } diff --git a/recipeBuddy/package.json b/recipeBuddy/package.json index e90b482..e33973c 100644 --- a/recipeBuddy/package.json +++ b/recipeBuddy/package.json @@ -17,7 +17,6 @@ "@angular/compiler": "~8.2.5", "@angular/core": "~8.2.5", "@angular/forms": "~8.2.5", - "@angular/material": "^8.2.3", "@angular/platform-browser": "~8.2.5", "@angular/platform-browser-dynamic": "~8.2.5", "@angular/router": "~8.2.5", @@ -30,6 +29,7 @@ "@angular/cli": "~8.3.4", "@angular/compiler-cli": "~8.2.5", "@angular/language-service": "~8.2.5", + "@angular/material": "^8.2.3", "@types/jasmine": "~3.3.8", "@types/jasminewd2": "~2.0.3", "@types/node": "~8.9.4", -- cgit v1.1 From 4612a506a8ccb7859e08ff75f9aef5f9de2b1aba Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 20:10:03 -0500 Subject: Add getRecipe to backend service --- recipeBuddy/src/app/REST_service/backend.service.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts index cc08bc6..9903ad0 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -47,6 +47,17 @@ export class BackendService { ) } + getRecipe(id): Observable + { + console.log(this.apiURL + '/recipes' + id) + return this.http.get(this.apiURL + '/recipes/' + id) + .pipe ( + retry(1), + map(msg => msg.Data), + catchError(this.handleError) + ) + } + handleError(error) { let errMsg = ''; if (error.error instanceof ErrorEvent) { -- cgit v1.1 From d70c3e598450a71ae3971770f63d13dbf6838952 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 22:53:23 -0500 Subject: Add more CORS related header management `Access-Control-Allow-Methods` & `Access-Control-Allow-Headers` headers are needed to make POST requests from angular. --- backend/main.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/main.go b/backend/main.go index 0bd5ebf..001a5cb 100644 --- a/backend/main.go +++ b/backend/main.go @@ -93,6 +93,11 @@ func RecipeList(w http.ResponseWriter, r *http.Request) { sendResponse(w, http.StatusCreated, "Recipe added successfully", recipe) + } else if r.Method == "OPTIONS" { + + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS") //Enable CORS + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") //Enable CORS + sendResponse(w, http.StatusOK, "Set Allowed Methods CORS", nil) } else { sendResponse(w, http.StatusMethodNotAllowed, "Invalid method", nil) @@ -183,6 +188,11 @@ func SingleRecipe(w http.ResponseWriter, r *http.Request) { "Recipe Deleted Successfully", nil) } + } else if r.Method == "OPTIONS" { + + w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS") //Enable CORS + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") //Enable CORS + sendResponse(w, http.StatusOK, "Set Allowed Methods CORS", nil) } else { sendResponse(w, http.StatusMethodNotAllowed, "Invalid method", nil) -- cgit v1.1 From b00429267a9bfe0a3e44290330fac299e00c2753 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 22:58:08 -0500 Subject: Add createRecipe to backend service --- recipeBuddy/src/app/REST_service/backend.service.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts index 9903ad0..e6456ce 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -58,6 +58,12 @@ export class BackendService { ) } + createRecipe(data): Observable + { + return this.http.post(this.apiURL + '/recipes', + JSON.stringify(data), this.httpOptions) + } + handleError(error) { let errMsg = ''; if (error.error instanceof ErrorEvent) { -- cgit v1.1 From 04c3b3fd7ffd2e377f7e443b072a86a332e94ac1 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 23:10:01 -0500 Subject: Add updateRecipe to backend --- recipeBuddy/src/app/REST_service/backend.service.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts index e6456ce..8ae05ca 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -64,6 +64,12 @@ export class BackendService { JSON.stringify(data), this.httpOptions) } + updateRecipe(data): Observable + { + return this.http.put(this.apiURL + '/recipes/' + data.id, + JSON.stringify(data), this.httpOptions) + } + handleError(error) { let errMsg = ''; if (error.error instanceof ErrorEvent) { -- cgit v1.1 From 658dea27e47e3aab24598a74ee9940f82bea2368 Mon Sep 17 00:00:00 2001 From: Tucker Evans Date: Mon, 2 Dec 2019 23:13:27 -0500 Subject: Add deleteRecipe to backend service --- recipeBuddy/src/app/REST_service/backend.service.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/recipeBuddy/src/app/REST_service/backend.service.ts b/recipeBuddy/src/app/REST_service/backend.service.ts index 8ae05ca..1a2d933 100644 --- a/recipeBuddy/src/app/REST_service/backend.service.ts +++ b/recipeBuddy/src/app/REST_service/backend.service.ts @@ -47,9 +47,9 @@ export class BackendService { ) } + getRecipe(id): Observable { - console.log(this.apiURL + '/recipes' + id) return this.http.get(this.apiURL + '/recipes/' + id) .pipe ( retry(1), @@ -70,6 +70,15 @@ export class BackendService { JSON.stringify(data), this.httpOptions) } + deleteRecipe(id): Observable + { + return this.http.delete(this.apiURL + '/recipes/' + id) + .pipe ( + retry(1), + catchError(this.handleError) + ) + } + handleError(error) { let errMsg = ''; if (error.error instanceof ErrorEvent) { -- cgit v1.1