update views + reduced providers in main app
[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 _getnameurl=_server+'/'+_db+'/'+_viewname;
15         static const _posturl=_server+'/'+_db;
16
17         final BrowserClient _http;
18
19         ProductCategoryService(this._http);
20
21         Future<List<ProductCategory>> getAll() async {
22                 try {
23                         List<ProductCategory> prodcats=[];
24                         final response = await _http.get(_getnameurl);
25                         for(var item in JSON.decode(response.body)['rows']) {
26                                 prodcats.add(new ProductCategory(
27                                         item['id'],
28                                         item['value']['name'],
29                                         item['value']['type']));
30                         }
31                         return prodcats;
32                 }
33                 catch(e) {
34                         throw _handleError(e);
35                 }
36         }
37
38         Future<String> createProdCategory(String name) async {
39                 try {
40                         var res = await _http.post(
41                                 _posturl,
42                                 headers: {'Content-Type': 'application/json'},
43                                 body: JSON.encode({
44                                         'name': name,
45                                         'type': 'product_category'
46                                 })
47                         );
48                         return(JSON.decode(res.body)['id']);
49                 }
50                 catch(e) {
51                         throw _handleError(e);
52                 }
53         }
54
55         Future<ProductCategory> getById(String id) async {
56                 try {
57                         ProductCategory prodcat;
58                         String url=_server+'/'+_db+'/'+id;
59                         final response = await _http.get(url);
60                         var item=JSON.decode(response.body);
61                         prodcat = new ProductCategory(
62                                 item['_id'],
63                                 item['name'],
64                                 item['type']
65                         );
66                         return prodcat;
67                 }
68                 catch(e) {
69                         throw _handleError(e);
70                 }
71         }
72
73         Future<Null> updateProdCategory(String id,String name) async {
74                 try {
75                         String url=_server+'/'+_db+'/'+id;
76                         final response = await _http.get(url);
77                         var resbody=JSON.decode(response.body);
78                         resbody['name']=name;
79                         await _http.put(
80                                 _posturl+'/'+id,
81                                 headers: {'Content-Type': 'application/json'},
82                                 body: JSON.encode(resbody)
83                         );
84                 }
85                 catch(e) {
86                         throw _handleError(e);
87                 }
88         }
89
90         Future<Null> deleteProdCategory(String id) async {
91                 try {
92                         var url=_server+'/'+_db+'/'+id;
93                         var response = await _http.get(url);
94                         var reso=JSON.decode(response.body);
95                         url=_server+'/'+_db+'/_purge/';
96                         final respurge = await _http.post(
97                                 url,
98                                 headers: {'Content-Type': 'application/json'},
99                                 body: JSON.encode({
100                                         reso['_id']: [reso['_rev']]
101                                 })
102                         );
103                         print('Debug: '+respurge.body);
104                 }
105                 catch(e) {
106                         throw _handleError(e);
107                 }
108         }
109
110         Exception _handleError(dynamic e) {
111                 print(e);
112                 return new Exception('Server error; cause: $e');
113         }
114
115 }
116