| @@ -1,6 +1,5 @@ | |||||
| import { BrowserModule } from '@angular/platform-browser'; | import { BrowserModule } from '@angular/platform-browser'; | ||||
| import { NgModule } from '@angular/core'; | import { NgModule } from '@angular/core'; | ||||
| import { HttpModule } from '@angular/http'; | |||||
| import { HttpClientModule } from '@angular/common/http'; | import { HttpClientModule } from '@angular/common/http'; | ||||
| import { AppComponent } from './app.component'; | import { AppComponent } from './app.component'; | ||||
| @@ -17,7 +16,6 @@ import { CoreModule } from './core/core.module'; | |||||
| imports: [ | imports: [ | ||||
| BrowserModule, | BrowserModule, | ||||
| HttpClientModule, | HttpClientModule, | ||||
| HttpModule, | |||||
| AppRoutingModule, | AppRoutingModule, | ||||
| SharedModule, | SharedModule, | ||||
| ShoppingListModule, | ShoppingListModule, | ||||
| @@ -1,4 +1,5 @@ | |||||
| import { NgModule } from "@angular/core"; | import { NgModule } from "@angular/core"; | ||||
| import { HTTP_INTERCEPTORS } from "@angular/common/http"; | |||||
| import { HeaderComponent } from "./header/header.component"; | import { HeaderComponent } from "./header/header.component"; | ||||
| import { HomeComponent } from "./home/home.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 { RecipeService } from "../recipes/recipe.service"; | ||||
| import { DataStorageService } from "../shared/data-storage.service"; | import { DataStorageService } from "../shared/data-storage.service"; | ||||
| import { AuthService } from "../auth/auth.service"; | import { AuthService } from "../auth/auth.service"; | ||||
| import { AuthInterceptor } from "../shared/auth.interceptor"; | |||||
| import { LoggingInterceptor } from "../shared/logging.interceptor"; | |||||
| @NgModule({ | @NgModule({ | ||||
| declarations: [ | declarations: [ | ||||
| @@ -26,7 +29,9 @@ import { AuthService } from "../auth/auth.service"; | |||||
| ShoppingListService, | ShoppingListService, | ||||
| RecipeService, | RecipeService, | ||||
| DataStorageService, | DataStorageService, | ||||
| AuthService | |||||
| AuthService, | |||||
| {provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}, | |||||
| {provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptor, multi: true} | |||||
| ], | ], | ||||
| }) | }) | ||||
| export class CoreModule {} | export class CoreModule {} | ||||
| @@ -13,7 +13,7 @@ export class HeaderComponent { | |||||
| onSaveData() { | onSaveData() { | ||||
| this.dataStorageService.storeRecipes().subscribe( | this.dataStorageService.storeRecipes().subscribe( | ||||
| response => { | response => { | ||||
| console.log(response); | |||||
| // console.log(response); | |||||
| } | } | ||||
| ); | ); | ||||
| } | } | ||||
| @@ -8,26 +8,7 @@ import { nextTick } from "q"; | |||||
| @Injectable() | @Injectable() | ||||
| export class RecipeService { | export class RecipeService { | ||||
| recipesChanged = new Subject<Recipe[]>(); | recipesChanged = new Subject<Recipe[]>(); | ||||
| 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) { } | constructor(private shoppingListService: ShoppingListService) { } | ||||
| @@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |||||
| const copiedReq = req.clone({ | |||||
| params: req.params.set('auth', this.authService.getToken()) | |||||
| }); | |||||
| return next.handle(copiedReq); | |||||
| } | |||||
| } | |||||
| @@ -1,26 +1,26 @@ | |||||
| import { Injectable } from '@angular/core'; | 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 { RecipeService } from '../recipes/recipe.service'; | ||||
| import { Recipe } from '../recipes/recipe.model'; | import { Recipe } from '../recipes/recipe.model'; | ||||
| import { AuthService } from '../auth/auth.service'; | |||||
| @Injectable() | @Injectable() | ||||
| export class DataStorageService { | export class DataStorageService { | ||||
| readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; | readonly baseUrl: string = 'https://my-recipe-book-cb837.firebaseio.com/'; | ||||
| constructor(private httpClient: HttpClient, | constructor(private httpClient: HttpClient, | ||||
| private recipeService: RecipeService, | |||||
| private authService: AuthService) {} | |||||
| private recipeService: RecipeService) {} | |||||
| storeRecipes() { | 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() { | fetchRecipes() { | ||||
| const token = this.authService.getToken(); | |||||
| this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json?auth=' + token).subscribe( | |||||
| this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json').subscribe( | |||||
| recipes => { | recipes => { | ||||
| this.recipeService.replaceRecipes(recipes); | this.recipeService.replaceRecipes(recipes); | ||||
| } | } | ||||
| @@ -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<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |||||
| // do without consuming it! | |||||
| return next.handle(req).do( | |||||
| event => { | |||||
| console.log('Logging interceptor', event); | |||||
| } | |||||
| ) | |||||
| } | |||||
| } | |||||
| @@ -4,10 +4,7 @@ import { Subject } from "rxjs/Subject"; | |||||
| export class ShoppingListService { | export class ShoppingListService { | ||||
| ingredientsChanged = new Subject<Ingredient[]>(); | ingredientsChanged = new Subject<Ingredient[]>(); | ||||
| startedEditing = new Subject<number>(); | startedEditing = new Subject<number>(); | ||||
| private ingredients: Ingredient[] = [ | |||||
| new Ingredient('Apples', 5), | |||||
| new Ingredient('Tomatoes', 10) | |||||
| ]; | |||||
| private ingredients: Ingredient[] = []; | |||||
| getIngredients() { | getIngredients() { | ||||
| return this.ingredients.slice(); | return this.ingredients.slice(); | ||||