2019-03-20 15:10:47 +00:00
|
|
|
// #docregion import-http
|
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient } from '@angular/common/http';
|
2021-07-01 09:11:56 -04:00
|
|
|
// #docregion props
|
2021-05-08 16:02:03 +02:00
|
|
|
import { Product } from './products';
|
2021-07-01 09:11:56 -04:00
|
|
|
// #enddocregion props, import-http
|
2021-02-20 16:30:31 +00:00
|
|
|
|
2019-03-20 15:10:47 +00:00
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
// #docregion props, methods, inject-http, get-shipping
|
|
|
|
export class CartService {
|
2021-02-20 16:30:31 +00:00
|
|
|
// #enddocregion get-shipping
|
2021-05-08 16:02:03 +02:00
|
|
|
items: Product[] = [];
|
2019-03-20 15:10:47 +00:00
|
|
|
// #enddocregion props, methods
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private http: HttpClient
|
|
|
|
) {}
|
|
|
|
// #enddocregion inject-http
|
|
|
|
// #docregion methods
|
|
|
|
|
2021-05-08 16:02:03 +02:00
|
|
|
addToCart(product: Product) {
|
2019-03-20 15:10:47 +00:00
|
|
|
this.items.push(product);
|
|
|
|
}
|
|
|
|
|
|
|
|
getItems() {
|
|
|
|
return this.items;
|
|
|
|
}
|
|
|
|
|
|
|
|
clearCart() {
|
|
|
|
this.items = [];
|
|
|
|
return this.items;
|
|
|
|
}
|
|
|
|
// #enddocregion methods
|
|
|
|
|
2021-02-20 16:30:31 +00:00
|
|
|
// #docregion get-shipping
|
2019-03-20 15:10:47 +00:00
|
|
|
getShippingPrices() {
|
2021-02-20 16:30:31 +00:00
|
|
|
return this.http.get<{type: string, price: number}[]>('/assets/shipping.json');
|
2019-03-20 15:10:47 +00:00
|
|
|
}
|
2019-04-11 09:44:50 -05:00
|
|
|
// #docregion props, methods, inject-http
|
2019-03-20 15:10:47 +00:00
|
|
|
}
|
2021-02-20 16:30:31 +00:00
|
|
|
// #enddocregion props, methods, inject-http
|