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<Null> createProdCategory(String name) async {
42 headers: {'Content-Type': 'application/json'},
45 'type': 'product_category'
50 throw _handleError(e);
54 Future<ProductCategory> getById(String id) async {
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(
68 throw _handleError(e);
72 Future<Null> updateProdCategory(String id,String name) async {
74 String url=_server+'/'+_db+'/'+id;
75 final response = await _http.get(url);
76 var resbody=JSON.decode(response.body);
80 headers: {'Content-Type': 'application/json'},
81 body: JSON.encode(resbody)
85 throw _handleError(e);
89 Future<Null> deleteProdCategory(String id) async {
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(
97 headers: {'Content-Type': 'application/json'},
99 reso['_id']: [reso['_rev']]
102 print('Debug: '+respurge.body);
105 throw _handleError(e);
109 Exception _handleError(dynamic e) {
111 return new Exception('Server error; cause: $e');