From 2801d3d9c90b1ac470de406506b8a58bfc78d7b5 Mon Sep 17 00:00:00 2001 From: Nils Dittberner Date: Tue, 28 Nov 2017 14:59:34 +0100 Subject: [PATCH] Temp commit --- src/app/app.module.ts | 2 -- src/app/core/core.module.ts | 7 ++++++- src/app/core/header/header.component.ts | 2 +- src/app/recipes/recipe.service.ts | 21 +-------------------- src/app/shared/auth.interceptor.ts | 16 ++++++++++++++++ src/app/shared/data-storage.service.ts | 16 ++++++++-------- src/app/shared/logging.interceptor.ts | 14 ++++++++++++++ src/app/shopping-list/shopping-list.service.ts | 5 +---- 8 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 src/app/shared/auth.interceptor.ts create mode 100644 src/app/shared/logging.interceptor.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 7c20811..9e779e7 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,6 +1,5 @@ import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; -import { HttpModule } from '@angular/http'; import { HttpClientModule } from '@angular/common/http'; import { AppComponent } from './app.component'; @@ -17,7 +16,6 @@ import { CoreModule } from './core/core.module'; imports: [ BrowserModule, HttpClientModule, - HttpModule, AppRoutingModule, SharedModule, ShoppingListModule, diff --git a/src/app/core/core.module.ts b/src/app/core/core.module.ts index 7592006..285be48 100644 --- a/src/app/core/core.module.ts +++ b/src/app/core/core.module.ts @@ -1,4 +1,5 @@ import { NgModule } from "@angular/core"; +import { HTTP_INTERCEPTORS } from "@angular/common/http"; import { HeaderComponent } from "./header/header.component"; import { HomeComponent } from "./home/home.component"; @@ -8,6 +9,8 @@ import { ShoppingListService } from "../shopping-list/shopping-list.service"; import { RecipeService } from "../recipes/recipe.service"; import { DataStorageService } from "../shared/data-storage.service"; import { AuthService } from "../auth/auth.service"; +import { AuthInterceptor } from "../shared/auth.interceptor"; +import { LoggingInterceptor } from "../shared/logging.interceptor"; @NgModule({ declarations: [ @@ -26,7 +29,9 @@ import { AuthService } from "../auth/auth.service"; ShoppingListService, RecipeService, DataStorageService, - AuthService + AuthService, + {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}, + {provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true} ], }) export class CoreModule {} \ No newline at end of file diff --git a/src/app/core/header/header.component.ts b/src/app/core/header/header.component.ts index 5f519bc..d888520 100644 --- a/src/app/core/header/header.component.ts +++ b/src/app/core/header/header.component.ts @@ -13,7 +13,7 @@ export class HeaderComponent { onSaveData() { this.dataStorageService.storeRecipes().subscribe( response => { - console.log(response); + // console.log(response); } ); } diff --git a/src/app/recipes/recipe.service.ts b/src/app/recipes/recipe.service.ts index 58e6ec3..d2a7c47 100644 --- a/src/app/recipes/recipe.service.ts +++ b/src/app/recipes/recipe.service.ts @@ -8,26 +8,7 @@ import { nextTick } from "q"; @Injectable() export class RecipeService { recipesChanged = new Subject(); - private recipes: Recipe[] = [ - new Recipe( - 'A Test Recipe', - 'This is simlpy a test', - 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg', - [ - new Ingredient('Foo', 1), - new Ingredient('Bar', 2) - ] - ), - new Recipe( - 'Another Test Recipe', - 'This is simlpy a test', - 'http://maxpixel.freegreatpicture.com/static/photo/1x/Recipe-Soup-Noodle-Curried-Spicy-Chicken-Khaosoi-2344152.jpg', - [ - new Ingredient('Foo', 1), - new Ingredient('Bar', 2) - ] - ) - ]; + private recipes: Recipe[] = []; constructor(private shoppingListService: ShoppingListService) { } diff --git a/src/app/shared/auth.interceptor.ts b/src/app/shared/auth.interceptor.ts new file mode 100644 index 0000000..7848c88 --- /dev/null +++ b/src/app/shared/auth.interceptor.ts @@ -0,0 +1,16 @@ +import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http' +import { Observable } from 'rxjs/Observable'; +import { Injectable } from '@angular/core'; +import { AuthService } from '../auth/auth.service'; + +@Injectable() +export class AuthInterceptor implements HttpInterceptor { + constructor(private authService: AuthService) {} + + intercept(req: HttpRequest, next: HttpHandler): Observable> { + const copiedReq = req.clone({ + params: req.params.set('auth', this.authService.getToken()) + }); + return next.handle(copiedReq); + } +} \ No newline at end of file diff --git a/src/app/shared/data-storage.service.ts b/src/app/shared/data-storage.service.ts index 09c7280..c21e898 100644 --- a/src/app/shared/data-storage.service.ts +++ b/src/app/shared/data-storage.service.ts @@ -1,26 +1,26 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http' +import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http' import { RecipeService } from '../recipes/recipe.service'; import { Recipe } from '../recipes/recipe.model'; -import { AuthService } from '../auth/auth.service'; @Injectable() export class DataStorageService { readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; constructor(private httpClient: HttpClient, - private recipeService: RecipeService, - private authService: AuthService) {} + private recipeService: RecipeService) {} storeRecipes() { - const token = this.authService.getToken(); - return this.httpClient.put(this.baseUrl + 'recipes.json?auth=' + token, this.recipeService.getRecipes()); + // const token = this.authService.getToken(); + // return this.httpClient.put(this.baseUrl + 'recipes.json?auth=' + token, this.recipeService.getRecipes()); + // const req = new HttpRequest('PUT', this.baseUrl, this.recipeService.getRecipes(), {reportProgress: true, params: new HttpParams().set('auth', token)}); + // return this.httpClient.request(req); + return this.httpClient.put(this.baseUrl + 'recipes.json', this.recipeService.getRecipes()); } fetchRecipes() { - const token = this.authService.getToken(); - this.httpClient.get(this.baseUrl + 'recipes.json?auth=' + token).subscribe( + this.httpClient.get(this.baseUrl + 'recipes.json').subscribe( recipes => { this.recipeService.replaceRecipes(recipes); } diff --git a/src/app/shared/logging.interceptor.ts b/src/app/shared/logging.interceptor.ts new file mode 100644 index 0000000..9aa5f46 --- /dev/null +++ b/src/app/shared/logging.interceptor.ts @@ -0,0 +1,14 @@ +import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from "@angular/common/http"; +import { Observable } from "rxjs/Observable"; +import 'rxjs/add/operator/do'; + +export class LoggingInterceptor implements HttpInterceptor { + intercept(req: HttpRequest, next: HttpHandler): Observable> { + // do without consuming it! + return next.handle(req).do( + event => { + console.log('Logging interceptor', event); + } + ) + } +} \ No newline at end of file diff --git a/src/app/shopping-list/shopping-list.service.ts b/src/app/shopping-list/shopping-list.service.ts index 935899b..2f3ba93 100644 --- a/src/app/shopping-list/shopping-list.service.ts +++ b/src/app/shopping-list/shopping-list.service.ts @@ -4,10 +4,7 @@ import { Subject } from "rxjs/Subject"; export class ShoppingListService { ingredientsChanged = new Subject(); startedEditing = new Subject(); - private ingredients: Ingredient[] = [ - new Ingredient('Apples', 5), - new Ingredient('Tomatoes', 10) - ]; + private ingredients: Ingredient[] = []; getIngredients() { return this.ingredients.slice();