import { Injectable , Inject , PLATFORM_ID } from '@angular/core' ; import { isPlatformBrowser } from '@angular/common' ; import { Observable } from 'rxjs' ; import { map } from 'rxjs/operators' ; import { HttpClient , HttpHeaders } from '@angular/common/http' ; import { NavService } from '../shared/nav.service' ; @ Injectable ( ) export class ProductService { baseUrl : string ; constructor ( private http , private navService , @ Inject ( 'BASE_URL' ) baseUrl , @ Inject ( PLATFORM_ID ) private platformId ) { this . baseUrl = baseUrl ; } getProductById ( id ) { const productUrl = `/api/product/${ id }` ; return this . http . get ( this . baseUrl + productUrl ) ; } addToCart ( id , quantity ) { const cartInfo = JSON . stringify ( { productId : id , productQuantity : quantity } ) ; const headers = new HttpHeaders ( { Accept : 'application/json' , 'Content-Type' : 'application/json' } ) ; return this . http . post ( `/api/user/cart/add` , cartInfo , { headers } ) . pipe ( map ( ( res ) => { if ( res . success ) { if ( isPlatformBrowser ( this . platformId ) ) { localStorage . setItem ( 'id_cart' , res . cart || 0 ) ; } this . navService . changeCart ( res . cart || 0 ) ; return true ; } return false ; } ) ) ; } }