|
1
|
|
|
import { environment } from "./../../environments/environment"; |
|
2
|
|
|
import { Injectable } from "@angular/core"; |
|
3
|
|
|
import { Observable } from "rxjs"; |
|
4
|
|
|
import { HttpClient } from "@angular/common/http"; |
|
5
|
|
|
import { Response } from "../models/response"; |
|
6
|
|
|
import { Product } from "../models/product"; |
|
7
|
|
|
@Injectable({ |
|
8
|
|
|
providedIn: "root" |
|
9
|
|
|
}) |
|
10
|
|
|
|
|
11
|
|
|
export class ProductsService { |
|
12
|
|
|
|
|
13
|
|
|
private baseUrl = `${environment.api+"products"+"?API_KEY="+environment.api_key}`; |
|
14
|
|
|
private baseUrlUpate = `${environment.api+"updateProducts"+"?API_KEY="+environment.api_key}`; |
|
15
|
|
|
|
|
16
|
|
|
constructor(private http: HttpClient) { } |
|
17
|
|
|
|
|
18
|
|
|
getProducts() : Observable<Response> { |
|
19
|
|
|
return this.http.get<Response>(this.baseUrl); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
addProduct(product: Product) : Observable<Response> { |
|
23
|
|
|
let params = new FormData(); |
|
24
|
|
|
params.append("name", product.name); |
|
25
|
|
|
params.append("description", product.description); |
|
26
|
|
|
params.append("price", `${product.price}`); |
|
27
|
|
|
params.append("stock", `${product.stock}`); |
|
28
|
|
|
params.append("category", `${product.Category}`); |
|
29
|
|
|
params.append("image", product.image); |
|
30
|
|
|
|
|
31
|
|
|
return this.http.post<Response>(this.baseUrl, params); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
editProduct(product: Product) : Observable<Response> { |
|
35
|
|
|
const url = this.baseUrlUpate+this.constructUrlParams(product); |
|
36
|
|
|
return this.http.get<Response>(url); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
constructUrlParams = (object: Product) => { |
|
40
|
|
|
let result = ''; |
|
41
|
|
|
for (const property in object) { |
|
42
|
|
|
result += `&${property}=${object[property]}`; |
|
43
|
|
|
} |
|
44
|
|
|
return result; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|