summaryrefslogtreecommitdiff
path: root/recipeBuddy/src/app/REST_service
diff options
context:
space:
mode:
authorTucker Evans <tuckerevans24@gmail.com>2019-12-07 14:27:26 -0500
committerGitHub <noreply@github.com>2019-12-07 14:27:26 -0500
commit5cf3689290666f70cd631d7b3d40a2ed245a3fce (patch)
treee6f2938377890774ff3e80fae8f77797917a46e5 /recipeBuddy/src/app/REST_service
parentcafe1b4fd3cc02554f44ffbaa8467d867a6838cb (diff)
parent658dea27e47e3aab24598a74ee9940f82bea2368 (diff)
Merge pull request #12 from tuckerevans/REST-service
Backend REST Service implementation
Diffstat (limited to 'recipeBuddy/src/app/REST_service')
-rw-r--r--recipeBuddy/src/app/REST_service/backend.service.spec.ts12
-rw-r--r--recipeBuddy/src/app/REST_service/backend.service.ts93
2 files changed, 105 insertions, 0 deletions
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);
+ }
+}