import { Injectable , Inject } from '@angular/core' ; import { HttpClient , HttpHeaders , HttpEvent , HttpRequest } from '@angular/common/http' ; import { Observable } from 'rxjs' ; import { Product } from '../Model/product' ; import { User } from '../Model/user' ; import { SESSION_STORAGE , StorageService } from 'angular-webstorage-service' ; import { Address } from '../Model/address' ; import { environment } from 'src/environments/environment' ; @ Injectable ( { providedIn : 'root' } ) export class ApiService { constructor ( @ Inject ( SESSION_STORAGE ) private storage , private http ) { } register ( user ) { return this . http . post ( environment . baseUrl + environment . signupUrl , JSON . stringify ( user ) , { headers : { 'Content-Type' : 'application/json' } } ) ; } login ( user ) { return this . http . post ( environment . baseUrl + environment . loginUrl , JSON . stringify ( user ) , { headers : { 'Content-Type' : 'application/json' } } ) ; } logout ( ) { this . http . get < any > ( environment . baseUrl + environment . logoutUrl ) ; } addToCart ( product ) { return this . http . get < any > ( environment . baseUrl + environment . addToCartUrl + "?productId=" + product . productid ) ; } getCartItems ( ) { return this . http . get < any > ( environment . baseUrl + environment . viewCartUrl ) ; } updateCartItem ( prodid , quant ) { var map = { "id" : prodid , "quantity" : quant } ; return this . http . put < any > ( environment . baseUrl + environment . updateCartUrl , map ) ; } deleteCartItem ( bufdid ) { return this . http . delete < any > ( environment . baseUrl + environment . deleteCartUrl + "?bufcartid=" + bufdid ) ; } addOrUpdateAddress ( adr ) { return this . http . post < any > ( environment . baseUrl + environment . addAddressUrl , adr ) ; } getAddress ( ) { return this . http . get < any > ( environment . baseUrl + environment . viewAddressUrl ) ; } getProducts ( ) { return this . http . get < any > ( environment . baseUrl + environment . productsUrl ) ; } addProduct ( desc , quan , price , prodname , image ) { const formData = new FormData ( ) ; formData . append ( "description" , desc ) ; formData . append ( "price" , price ) ; formData . append ( "productname" , prodname ) ; formData . append ( "quantity" , quan ) ; formData . append ( "file" , image ) ; return this . http . post < any > ( environment . baseUrl + environment . addProductUrl , formData ) ; } updateProduct ( desc , quan , price , prodname , image , productid ) { const formData = new FormData ( ) ; formData . append ( "description" , desc ) ; formData . append ( "price" , price ) ; formData . append ( "productname" , prodname ) ; formData . append ( "quantity" , quan ) ; formData . append ( "file" , image ) ; formData . append ( "productId" , productid ) ; return this . http . put < any > ( environment . baseUrl + environment . updateProductUrl , formData ) ; } deleteProduct ( prodid ) { return this . http . delete < any > ( environment . baseUrl + environment . deleteProductUrl + "?productId=" + prodid ) ; } getOrders ( ) { return this . http . get < any > ( environment . baseUrl + environment . viewOrderUrl ) ; } placeOrder ( ) { return this . http . get < any > ( environment . baseUrl + environment . placeOrderUrl ) ; } updateStatusForOrder ( order ) { const formData = new FormData ( ) ; formData . append ( "orderId" , order . orderId ) ; formData . append ( "orderStatus" , order . orderStatus ) ; return this . http . post < any > ( environment . baseUrl + environment . updateOrderUrl , formData ) ; } isAuthenticated ( ) { return this . getToken ( ) !== null ; } storeToken ( token , auth_type ) { this . storage . set ( "auth_token" , token ) ; this . storage . set ( "auth_type" , auth_type ) ; } getAuthType ( ) { if ( this . storage . get ( "auth_type" ) !== null ) { return this . storage . get ( "auth_type" ) ; } return null ; } getToken ( ) { return this . storage . get ( "auth_token" ) ; } removeToken ( ) { this . storage . remove ( "auth_type" ) ; return this . storage . remove ( "auth_token" ) ; } }