41af5cb9e3f81ad1d3c8cffeed3dc09397cc1d4c
[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<Null> createProduct(String name,double price,String cat) async {
61                 try {
62                         var url=_server+'/'+_db;
63                         await _http.post(
64                                 url,
65                                 headers: {'Content-Type': 'application/json'},
66                                 body: JSON.encode({
67                                         'name': name,
68                                         'price': price,
69                                         'type': 'product',
70                                         'category': cat
71                                 })
72                         );
73                 }
74                 catch(e) {
75                         throw _handleError(e);
76                 }
77         }
78
79         Future<Null> deleteProduct(String id) async {
80                 try {
81                         var url=_server+'/'+_db+'/'+id;
82                         var resp = await _http.get(url);
83                         var reso=JSON.decode(resp.body);
84                         url=_server+'/'+_db+'/_purge/';
85                         await _http.post(
86                                 url,
87                                 headers: {'Content-Type': 'application/json'},
88                                 body: JSON.encode({
89                                         reso['_id']: [reso['_rev']]
90                                 })
91                         );
92                 }
93                 catch(e) {
94                         throw _handleError(e);
95                 }
96         }
97
98         Future<Product> getById(String id) async {
99                 try {
100                         var url=_server+'/'+_db+'/'+id;
101                         Product prod;
102                         final response = await _http.get(url);
103                         var item=JSON.decode(response.body);
104                         prod=(new Product(item['_id'],item['name'],
105                                           item['price'],item['type'],
106                                           item['category']));
107                         return prod;
108                 }
109                 catch(e) {
110                         throw _handleError(e);
111                 }
112         }
113
114         Future<Null> updateProd(String id,String name,double price,
115                                 String category) async {
116                 try {
117                         var url=_server+'/'+_db+'/'+id;
118                         var resp = await _http.get(url);
119                         var reso=JSON.decode(resp.body);
120                         reso['name']=name;
121                         reso['price']=price;
122                         reso['category']=category;
123                         await _http.put(
124                                 url,
125                                 headers: {'Content-Type': 'application/json'},
126                                 body: JSON.encode(reso)
127                         );
128                 }
129                 catch(e) {
130                         throw _handleError(e);
131                 }
132         }
133
134         Exception _handleError(dynamic e) {
135                 print(e);
136                 return new Exception('Server error; cause: $e');
137         }
138
139 }
140