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 ++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 recipeBuddy/src/app/REST_service/backend.service.spec.ts create mode 100644 recipeBuddy/src/app/REST_service/backend.service.ts (limited to 'recipeBuddy/src/app/REST_service') 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); + } +} -- 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(+) (limited to 'recipeBuddy/src/app/REST_service') 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(-) (limited to 'recipeBuddy/src/app/REST_service') 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 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(+) (limited to 'recipeBuddy/src/app/REST_service') 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 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(+) (limited to 'recipeBuddy/src/app/REST_service') 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(+) (limited to 'recipeBuddy/src/app/REST_service') 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(-) (limited to 'recipeBuddy/src/app/REST_service') 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