ed08975fdc6f6f47b340641ee8765ea3e5085621
[outofuni/tavern2.git] / lib / product_component.dart
1 // Copyright (c) 2016, hackbard. All rights reserved. Use of this source code
2 // is governed by a BSD-style license that can be found in the LICENSE file.
3
4 import 'dart:async';
5
6 import 'package:angular2/core.dart';
7 import 'package:angular2/router.dart';
8
9 import 'product.dart';
10 import 'product_service.dart';
11 import 'product_category.dart';
12 import 'product_category_service.dart';
13
14 import 'package:angular2_rbi/directives.dart';
15
16 @Component(
17         selector: 'my-products',
18         templateUrl: 'product_component.html',
19         styleUrls: const ['product_component.css'],
20         directives: const [MaterialTextfield,MaterialButton],
21         providers: const [ProductService,ProductCategoryService]
22 )
23
24 class ProductComponent implements OnInit {
25         final ProductService _prodSrv;
26         final ProductCategoryService _prodcatSrv;
27         final RouteParams _routeParams;
28         final Router _router;
29
30         String prod_category_id;
31         String prod_category_name;
32         String prod_name;
33         double prod_price;
34         int prodcnt;
35
36         List<Product> products;
37         ProductCategory prodcat;
38
39         ProductComponent(this._prodSrv,this._prodcatSrv,
40                          this._routeParams,this._router) {
41                 prod_category_name='Category';
42                 prod_category_id='ID';
43         }
44
45         Future<Null> ngOnInit() async {
46                 var catid=_routeParams.get('id');
47                 if(catid!=null) {
48                         products = await (_prodSrv.getByCategory(catid));
49                         prodcat = await (_prodcatSrv.getById(catid));
50                 }
51                 prod_category_name=prodcat.name;
52                 prod_category_id=prodcat.id;
53                 prodcnt=products.length;
54         }
55
56         Future<Null> updateProductCategory() async {
57                 bool doupdate=false;
58                 bool changecat=false;
59                 if(prodcat.id!=prod_category_id) {
60                         doupdate=true;
61                         changecat=true;
62                 }
63                 if(prodcat.name!=prod_category_name) {
64                         doupdate=true;
65                 }
66                 if(doupdate) {
67                         if(changecat) {
68                                 for(Product prod in products) {
69                                         print('Debug: change category of '+
70                                               prod.name);
71                                         await _prodSrv.updateProd(
72                                                 prod.id,
73                                                 prod.name,
74                                                 prod.price,
75                                                 prod_category_id
76                                         );
77                                 }
78                         }
79                         print('Debug: Updating product category '+
80                               prodcat.name+'/'+prodcat.id+' -> '+
81                               prod_category_name+'/'+prod_category_id);
82                         await _prodcatSrv.updateProdCategory(
83                                 prodcat.id,
84                                 prod_category_name,
85                                 prod_category_id
86                         );
87                 }
88         }
89
90         Future<Null> deleteProductCategory() async {
91                 bool delete=false;
92                 if(products.length==0) {
93                         delete=true;
94                 }
95                 else {
96                         print('Debug: Not deleting anything!');
97                 }
98                 if(delete) {
99                         print('Debug: Deleting product category '+
100                               prodcat.name+'/'+prodcat.id);
101                         await _prodcatSrv.deleteProdCategory(prodcat.doc_id);
102                 }
103         }
104
105         Future<Null> createProduct() async {
106                 if(prod_name==null || prod_price==null)
107                         return;
108                 print('Debug: Creating product '+prod_name+'/'+
109                       prod_price.toString());
110                 await _prodSrv.createProduct(prod_name,
111                                              double.parse(prod_price),
112                                              prod_category_id);
113         }
114
115         choose(Product prod) {
116                 goto_product(prod);
117         }
118
119         Future<Null> goto_product(Product prod) => _router.navigate([
120                 'ProductDetail',
121                 {'id': prod.id.toString()}
122         ]);
123
124 }
125