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 (limited to 'recipeBuddy/src/app') 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