import { Injectable } from '@angular/core'; import * as global from '../../core/common'; import { Http, RequestOptions, URLSearchParams, Response } from '@angular/http'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/catch'; import 'rxjs/add/operator/map'; import 'rxjs/add/observable/throw'; import { Observable, throwError } from 'rxjs' import { Product } from '../../core/products/Products'; import { ProductInventory } from '../../core/products/ProductInventory'; import { ProductCategories } from '../../core/products/ProductCategories'; import { ProductPermisions } from '../../core/products/ProductPermisions'; import { ProductImage } from '../../core/products/ProductImage'; import { ProductTags } from '../../core/products/ProductTags'; @Injectable({ providedIn: 'root' }) export class ProductsService { private GetProducts = global.serverBase + '/api/Products/GetProducts'; private GetProductInventoriesByProductId = global.serverBase + '/api/Products/GetInventoryByProductId'; private GetProductCategories = global.serverBase + '/api/Products/GetProductCategories'; private SetNewProduct = global.serverBase + '/api/Products/CreateNewProduct'; private SetNewProductPermisions = global.serverBase + '/api/Products/CreateNewProductPermisions'; private SetNewProductImages = global.serverBase + '/api/Products/CreateNewProductImages'; private SetNewProductTags = global.serverBase + '/api/Products/CreateNewProductTags'; private SetNewCategoriesOfProducts = global.serverBase + '/api/Products/CreateCategoriesForProducts'; private UpdateProducts = global.serverBase + '/api/Products/UpdateProduct'; private SetCSV = global.serverBase + '/api/Products/ImportProducts'; private SetZIP = global.serverBase + '/api/Products/ImportZipWithImages'; private ExportToCSV = global.serverBase + '/api/Products/ExportToCSV'; private GetAllCountries = global.countryServerBase; constructor(private http: Http) { } //Get countries GetCountries() { return this.http.get(this.GetAllCountries) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Get all products GetAllProducts(companyId: number, actionType: string = "All") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetProducts, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Get product inventories by product id GetProductInventories(companyId: number, id: number, actionType: string = "All") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); myparams.append('id', id.toString()); const options = myparams.toString(); return this.http.get(this.GetProductInventoriesByProductId, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create lnew product CreateNewProduct(product, companyId: number, actionType: string = "SET") { console.log('post test', product, actionType) const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetNewProduct, product, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } //Get product categories GetAllProductCategories(companyId: number, actionType: string = "All") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.GetProductCategories, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create new product permisions CreateNewProductPermisions(productPermisions, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetNewProductPermisions, productPermisions, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create new product images CreateNewProductImages(productImages, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetNewProductImages, productImages, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create new product images CreateNewProductTags(productTag, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetNewProductTags, productTag, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Create categories of products CreateCategoriesOfProducts(categoryOfProduct, companyId: number, actionType: string = "SET") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetNewCategoriesOfProducts, categoryOfProduct, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Update lnew product UpdateProduct(product, companyId: number, actionType: string = "UPDATE") { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.UpdateProducts, product, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Save imported CSV SaveImportedCSV(csv, companyId: number, actionType: string) { const myparams = new URLSearchParams(); myparams.append('csv', csv.toString()); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetCSV, csv, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Save zip file SaveImportedZip(zip, companyId: number, actionType: string) { const myparams = new URLSearchParams(); myparams.append('csv', zip.toString()); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType.toString()); let options = new RequestOptions({ params: myparams }); return this.http.post(this.SetZIP, zip, options) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } // Export products ExportProducts(companyId: number, actionType: string) { const myparams = new URLSearchParams(); myparams.append('companyId', companyId.toString()); myparams.append('actionType', actionType); const options = myparams.toString(); return this.http.get(this.ExportToCSV, { params: options }) // tslint:disable-next-line:no-angle-bracket-type-assertion .map((response: Response) => response.json()) .catch(this.handleError); } private handleError(error): Observable { const message = error.statusText || 'Server error'; return throwError(error); } }