cleaups and content page refresh (in progress)
[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<Null> createProdCategory(String name) async {
39                 try {
40                         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                 }
49                 catch(e) {
50                         throw _handleError(e);
51                 }
52         }
53
54         Future<ProductCategory> getById(String id) async {
55                 try {
56                         ProductCategory prodcat;
57                         String url=_server+'/'+_db+'/'+id;
58                         final response = await _http.get(url);
59                         var item=JSON.decode(response.body);
60                         prodcat = new ProductCategory(
61                                 item['_id'],
62                                 item['name'],
63                                 item['type']
64                         );
65                         return prodcat;
66                 }
67                 catch(e) {
68                         throw _handleError(e);
69                 }
70         }
71
72         Future<Null> updateProdCategory(String id,String name) async {
73                 try {
74                         String url=_server+'/'+_db+'/'+id;
75                         final response = await _http.get(url);
76                         var resbody=JSON.decode(response.body);
77                         resbody['name']=name;
78                         await _http.put(
79                                 _posturl+'/'+id,
80                                 headers: {'Content-Type': 'application/json'},
81                                 body: JSON.encode(resbody)
82                         );
83                 }
84                 catch(e) {
85                         throw _handleError(e);
86                 }
87         }
88
89         Future<Null> deleteProdCategory(String id) async {
90                 try {
91                         var url=_server+'/'+_db+'/'+id;
92                         var response = await _http.get(url);
93                         var reso=JSON.decode(response.body);
94                         url=_server+'/'+_db+'/_purge/';
95                         final respurge = await _http.post(
96                                 url,
97                                 headers: {'Content-Type': 'application/json'},
98                                 body: JSON.encode({
99                                         reso['_id']: [reso['_rev']]
100                                 })
101                         );
102                         print('Debug: '+respurge.body);
103                 }
104                 catch(e) {
105                         throw _handleError(e);
106                 }
107         }
108
109         Exception _handleError(dynamic e) {
110                 print(e);
111                 return new Exception('Server error; cause: $e');
112         }
113
114 }
115