c62e7ef0e18e2475141e5afbcf54c8a4d867c475
[outofuni/tavern2.git] / lib / product_category_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_category.dart';
8
9 @Injectable()
10 class ProductCategoryService {
11         static const _server='http://10.8.0.1:5984';
12         static const _db='tavern';
13         static const _viewname='_design/product_categories/_view/byname';
14         static const _viewid='_design/product_categories/_view/byid';
15         static const _getnameurl=_server+'/'+_db+'/'+_viewname;
16         static const _getidurl=_server+'/'+_db+'/'+_viewid;
17         static const _posturl=_server+'/'+_db;
18
19         final BrowserClient _http;
20
21         ProductCategoryService(this._http);
22
23         Future<List<ProductCategory>> getAll() async {
24                 try {
25                         List<ProductCategory> prodcats=[];
26                         final response = await _http.get(_getnameurl);
27                         for(var item in JSON.decode(response.body)['rows']) {
28                                 prodcats.add(new ProductCategory(
29                                         item['id'],
30                                         item['value']['name'],
31                                         item['value']['id'],
32                                         item['value']['type']));
33                         }
34                         return prodcats;
35                 }
36                 catch(e) {
37                         throw _handleError(e);
38                 }
39         }
40
41         Future<Null> createProdCategory(String name,String id) async {
42                 try {
43                         await _http.post(
44                                 _posturl,
45                                 headers: {'Content-Type': 'application/json'},
46                                 body: JSON.encode({
47                                         'name': name,
48                                         'id': id,
49                                         'type': 'product_category'
50                                 })
51                         );
52                 }
53                 catch(e) {
54                         throw _handleError(e);
55                 }
56         }
57
58         Future<ProductCategory> getById(String id) async {
59                 try {
60                         ProductCategory prodcat;
61                         String url=_getidurl+'?key="'+id+'"';
62                         final response = await _http.get(url);
63                         var item=JSON.decode(response.body);
64                         prodcat = new ProductCategory(
65                                 item['rows'][0]['value']['_id'],
66                                 item['rows'][0]['value']['name'],
67                                 item['rows'][0]['value']['id'],
68                                 item['rows'][0]['value']['type']
69                         );
70                         return prodcat;
71                 }
72                 catch(e) {
73                         throw _handleError(e);
74                 }
75         }
76
77         Future<Null> updateProdCategory(String oldid,String name,
78                                         String id) async {
79                 try {
80                         String url=_getidurl+'?key="'+oldid+'"';
81                         final response = await _http.get(url);
82                         print('Debug UPDATE GET URL: '+url);
83                         print('Debug UPDATE GET response: '+response.body);
84                         var resbody=JSON.decode(response.body);
85                         resbody['rows'][0]['value']['name']=name;
86                         resbody['rows'][0]['value']['id']=id;
87                         final response_put = await _http.put(
88                                 _posturl+'/'+resbody['rows'][0]['id'],
89                                 headers: {'Content-Type': 'application/json'},
90                                 body: JSON.encode(resbody['rows'][0]['value'])
91                         );
92                         print('Debug UPDATE PUT response: '+response_put.body);
93                 }
94                 catch(e) {
95                         throw _handleError(e);
96                 }
97         }
98
99         Future<Null> deleteProdCategory(String doc_id) async {
100                 try {
101                         var url=_server+'/'+_db+'/'+doc_id;
102                         var response = await _http.get(url);
103                         var reso=JSON.decode(response.body);
104                         url=_server+'/'+_db+'/_purge/';
105                         final respurge = await _http.post(
106                                 url,
107                                 headers: {'Content-Type': 'application/json'},
108                                 body: JSON.encode({
109                                         reso['_id']: [reso['_rev']]
110                                 })
111                         );
112                         print('Debug: '+respurge.body);
113                 }
114                 catch(e) {
115                         throw _handleError(e);
116                 }
117         }
118
119         Exception _handleError(dynamic e) {
120                 print(e);
121                 return new Exception('Server error; cause: $e');
122         }
123
124 }
125