浏览代码

Temp commit

tags/before_ngrx
Nils Dittberner 8 年前
父节点
当前提交
2801d3d9c9
共有 8 个文件被更改,包括 47 次插入36 次删除
  1. +0
    -2
      src/app/app.module.ts
  2. +6
    -1
      src/app/core/core.module.ts
  3. +1
    -1
      src/app/core/header/header.component.ts
  4. +1
    -20
      src/app/recipes/recipe.service.ts
  5. +16
    -0
      src/app/shared/auth.interceptor.ts
  6. +8
    -8
      src/app/shared/data-storage.service.ts
  7. +14
    -0
      src/app/shared/logging.interceptor.ts
  8. +1
    -4
      src/app/shopping-list/shopping-list.service.ts

+ 0
- 2
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,


+ 6
- 1
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 {}

+ 1
- 1
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);
}
);
}


+ 1
- 20
src/app/recipes/recipe.service.ts 查看文件

@@ -8,26 +8,7 @@ import { nextTick } from "q";
@Injectable()
export class RecipeService {
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) { }



+ 16
- 0
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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const copiedReq = req.clone({
params: req.params.set('auth', this.authService.getToken())
});
return next.handle(copiedReq);
}
}

+ 8
- 8
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<Recipe[]>(this.baseUrl + 'recipes.json?auth=' + token).subscribe(
this.httpClient.get<Recipe[]>(this.baseUrl + 'recipes.json').subscribe(
recipes => {
this.recipeService.replaceRecipes(recipes);
}


+ 14
- 0
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<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// do without consuming it!
return next.handle(req).do(
event => {
console.log('Logging interceptor', event);
}
)
}
}

+ 1
- 4
src/app/shopping-list/shopping-list.service.ts 查看文件

@@ -4,10 +4,7 @@ import { Subject } from "rxjs/Subject";
export class ShoppingListService {
ingredientsChanged = new Subject<Ingredient[]>();
startedEditing = new Subject<number>();
private ingredients: Ingredient[] = [
new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10)
];
private ingredients: Ingredient[] = [];

getIngredients() {
return this.ingredients.slice();


正在加载...
取消
保存