218b16937ae027cfd302425ef52113aa03e984a5
[outofuni/tavern2.git] / lib / product_service.dart
1 import 'dart:async';
2 import 'dart:convert';
3
4 import 'package:angular2/core.dart';
5 import 'package:http/browser_client.dart';
6
7 import 'product.dart';
8
9 @Injectable()
10 class ProductService {
11         final BrowserClient _http;
12
13         static const _server='http://10.8.0.1:5984';
14         static const _db='tavern';
15         static const _byname='_design/products/_view/byname';
16         static const _bycategory='_design/products/_view/bycategory';
17
18         ProductService(this._http);
19
20         Future<List<Product>> getAll() async {
21                 try {
22                         var url=_server+'/'+_db+'/'+_byname;
23                         List<Product> prods=[];
24                         final response = await _http.get(url);
25                         for(var item in JSON.decode(response.body)['rows']) {
26                                 prods.add(new Product(item['id'],
27                                                       item['value']['name'],
28                                                       item['value']['price'],
29                                                       item['value']['type'],
30                                                       item['value']['category']
31                                 ));
32                         }
33                         return prods;
34                 }
35                 catch(e) {
36                         throw _handleError(e);
37                 }
38         }
39
40         Future<List<Product>> getByCategory(String id) async {
41                 try {
42                         var url=_server+'/'+_db+'/'+_bycategory+'?key="'+id+'"';
43                         List<Product> prods=[];
44                         final response = await _http.get(url);
45                         for(var item in JSON.decode(response.body)['rows']) {
46                                 prods.add(new Product(item['id'],
47                                                       item['value']['name'],
48                                                       item['value']['price'],
49                                                       item['value']['type'],
50                                                       item['value']['category']
51                                 ));
52                         }
53                         return prods;
54                 }
55                 catch(e) {
56                         throw _handleError(e);
57                 }
58         }
59
60         Future<String> createProduct(String name,double pri,String cat) async {
61                 try {
62                         var url=_server+'/'+_db;
63                         var response = await _http.post(
64                                 url,
65                                 headers: {'Content-Type': 'application/json'},
66                                 body: JSON.encode({
67                                         'name': name,
68                                         'price': pri,
69                                         'type': 'product',
70                                         'category': cat
71                                 })
72                         );
73                         return(JSON.decode(response.body)['id']);
74                 }
75                 catch(e) {
76                         throw _handleError(e);
77                 }
78         }
79
80         Future<Null> deleteProduct(String id) async {
81                 try {
82                         var url=_server+'/'+_db+'/'+id;
83                         var resp = await _http.get(url);
84                         var reso=JSON.decode(resp.body);
85                         url=_server+'/'+_db+'/_purge/';
86                         await _http.post(
87                                 url,
88                                 headers: {'Content-Type': 'application/json'},
89                                 body: JSON.encode({
90                                         reso['_id']: [reso['_rev']]
91                                 })
92                         );
93                 }
94                 catch(e) {
95                         throw _handleError(e);
96                 }
97         }
98
99         Future<Product> getById(String id) async {
100                 try {
101                         var url=_server+'/'+_db+'/'+id;
102                         Product prod;
103                         final response = await _http.get(url);
104                         var item=JSON.decode(response.body);
105                         prod=(new Product(item['_id'],item['name'],
106                                           item['price'],item['type'],
107                                           item['category']));
108                         return prod;
109                 }
110                 catch(e) {
111                         throw _handleError(e);
112                 }
113         }
114
115         Future<Null> updateProd(String id,String name,double price,
116                                 String category) async {
117                 try {
118                         var url=_server+'/'+_db+'/'+id;
119                         var resp = await _http.get(url);
120                         var reso=JSON.decode(resp.body);
121                         reso['name']=name;
122                         reso['price']=price;
123                         reso['category']=category;
124                         await _http.put(
125                                 url,
126                                 headers: {'Content-Type': 'application/json'},
127                                 body: JSON.encode(reso)
128                         );
129                 }
130                 catch(e) {
131                         throw _handleError(e);
132                 }
133         }
134
135         Exception _handleError(dynamic e) {
136                 print(e);
137                 return new Exception('Server error; cause: $e');
138         }
139
140 }
141