4 import 'package:angular2/core.dart';
5 import 'package:http/browser_client.dart';
7 import 'product_category.dart';
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;
17 final BrowserClient _http;
19 ProductCategoryService(this._http);
21 Future<List<ProductCategory>> getAll() async {
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(
28 item['value']['name'],
29 item['value']['type']));
34 throw _handleError(e);
38 Future<String> createProdCategory(String name) async {
40 var res = await _http.post(
42 headers: {'Content-Type': 'application/json'},
45 'type': 'product_category'
48 return(JSON.decode(res.body)['id']);
51 throw _handleError(e);
55 Future<ProductCategory> getById(String id) async {
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(
69 throw _handleError(e);
73 Future<Null> updateProdCategory(String id,String name) async {
75 String url=_server+'/'+_db+'/'+id;
76 final response = await _http.get(url);
77 var resbody=JSON.decode(response.body);
81 headers: {'Content-Type': 'application/json'},
82 body: JSON.encode(resbody)
86 throw _handleError(e);
90 Future<Null> deleteProdCategory(String id) async {
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(
98 headers: {'Content-Type': 'application/json'},
100 reso['_id']: [reso['_rev']]
103 print('Debug: '+respurge.body);
106 throw _handleError(e);
110 Exception _handleError(dynamic e) {
112 return new Exception('Server error; cause: $e');