From 745a762fbeecd2ae0ff0672030b328b2ec3a3822 Mon Sep 17 00:00:00 2001 From: Alex Hebing Date: Tue, 19 Nov 2019 09:09:20 +0100 Subject: [PATCH] Consume backend example app in frontend (#11) --- .../src/app/home/home.component.ts | 6 ++--- .../src/app/services/backend.service.ts | 22 ++++++++----------- 2 files changed, 11 insertions(+), 17 deletions(-) diff --git a/{{cookiecutter.slug}}/frontend.angular/src/app/home/home.component.ts b/{{cookiecutter.slug}}/frontend.angular/src/app/home/home.component.ts index 3a1569c..6ac4382 100644 --- a/{{cookiecutter.slug}}/frontend.angular/src/app/home/home.component.ts +++ b/{{cookiecutter.slug}}/frontend.angular/src/app/home/home.component.ts @@ -12,10 +12,8 @@ export class HomeComponent implements OnInit { constructor(private backend: BackendService) { } ngOnInit() { - // This is just an example call to /api/whatever - // Note that the backend service doesn't call the backend yet - // and simply returns some mock data - this.backend.get('whatever').then(hoorays => { + // This is just an example call to /api/example/ + this.backend.get('example').then(hoorays => { if (hoorays.length) { this.hooray = hoorays[0].message; } diff --git a/{{cookiecutter.slug}}/frontend.angular/src/app/services/backend.service.ts b/{{cookiecutter.slug}}/frontend.angular/src/app/services/backend.service.ts index 98dad67..fea2b1a 100644 --- a/{{cookiecutter.slug}}/frontend.angular/src/app/services/backend.service.ts +++ b/{{cookiecutter.slug}}/frontend.angular/src/app/services/backend.service.ts @@ -11,27 +11,23 @@ export class BackendService { constructor(private config: ConfigService, private http: HttpClient) { } - /** + /** * Collect JSON from an specific url. * @param objectUrl The part of the URL after the backendUrl from config.json. - * (i.e. whatever comes after, for example, '/api/') + * (i.e. whatever comes after, for example, '/api/'). + * Note that this method will add a '/' at the end of the url if it does not exist. */ get(objectUrl: string): Promise { return this.getApiUrl().then(baseUrl => { if (!objectUrl.endsWith('/')) { objectUrl = `${objectUrl}/`; } const url: string = encodeURI(baseUrl + objectUrl); - // TODO: remove this part and enable below to actually contact the backend - return Promise.resolve([{ - message: 'https://media.giphy.com/media/yoJC2GnSClbPOkV0eA/source.gif' - }]); - - // return this.http.get(url) - // .toPromise() - // .then(response => { - // return response; - // }) - // .catch(this.handleError); + return this.http.get(url) + .toPromise() + .then(response => { + return response; + }) + .catch(this.handleError); }); }