import { Store } from "@ngrx/store" ; import { Injectable } from "@angular/core" ; import { Response , Headers , RequestOptionsArgs , RequestOptions , Http } from "@angular/http" ; import { Wine } from "../entities/Wine" ; import { ApplicationState } from "../../statemanagement/state/ApplicationState" ; import { API_URL } from "../../configuration" ; import { Observable } from "rxjs/Observable" ; @ Injectable ( ) export class StockService { private jwtToken : string ; constructor ( private store , private http ) { this . store . select ( ( state ) => state . data . authentication . jwtToken ) . subscribe ( ( jwtToken ) => this . jwtToken = jwtToken ) ; } add ( wine ) { return this . http . post ( `${ API_URL }/wines` , wine , this . authorizedHttpOptions ( ) ) . map ( ( res ) => res . json ( ) ) ; } update ( id , wine ) { return this . http . put ( `${ API_URL }/wines/${ id }` , wine , this . authorizedHttpOptions ( ) ) ; } remove ( wine ) { return this . http . delete ( `${ API_URL }/wines/${ wine . _id }` , this . authorizedHttpOptions ( ) ) ; } load ( ) { return this . http . get ( `${ API_URL }/wines` , this . authorizedHttpOptions ( ) ) . map ( ( res ) => res . json ( ) ) ; } fetchWine ( id ) { return this . http . get ( `${ API_URL }/wines/${ id }` , this . authorizedHttpOptions ( ) ) . map ( ( res ) => res . json ( ) ) ; } setRate ( wine , myRating ) { let newWine = Object . assign ( { } , wine , { myRating : myRating } ) ; return this . http . put ( `${ API_URL }/wines/${ wine . _id }` , newWine , this . authorizedHttpOptions ( ) ) ; } setStock ( wine , inStock ) { let newWine = Object . assign ( { } , wine , { inStock : inStock } ) ; return this . http . put ( `${ API_URL }/wines/${ wine . _id }` , newWine , this . authorizedHttpOptions ( ) ) ; } private authorizedHttpOptions ( ) { let headers = new Headers ( { authorization : `Bearer ${ this . jwtToken }` } ) ; return new RequestOptions ( { headers : headers } ) ; } }