static const _server='http://10.8.0.1:5984';
static const _db='tavern';
static const _viewname='_design/product_categories/_view/byname';
- static const _viewid='_design/product_categories/_view/byid';
static const _getnameurl=_server+'/'+_db+'/'+_viewname;
- static const _getidurl=_server+'/'+_db+'/'+_viewid;
static const _posturl=_server+'/'+_db;
final BrowserClient _http;
prodcats.add(new ProductCategory(
item['id'],
item['value']['name'],
- item['value']['id'],
item['value']['type']));
}
return prodcats;
}
}
- Future<Null> createProdCategory(String name,String id) async {
+ Future<String> createProdCategory(String name) async {
try {
- await _http.post(
+ var res = await _http.post(
_posturl,
headers: {'Content-Type': 'application/json'},
body: JSON.encode({
'name': name,
- 'id': id,
'type': 'product_category'
})
);
+ return(JSON.decode(res.body)['id']);
}
catch(e) {
throw _handleError(e);
Future<ProductCategory> getById(String id) async {
try {
ProductCategory prodcat;
- String url=_getidurl+'?key="'+id+'"';
+ String url=_server+'/'+_db+'/'+id;
final response = await _http.get(url);
var item=JSON.decode(response.body);
prodcat = new ProductCategory(
- item['rows'][0]['value']['_id'],
- item['rows'][0]['value']['name'],
- item['rows'][0]['value']['id'],
- item['rows'][0]['value']['type']
+ item['_id'],
+ item['name'],
+ item['type']
);
return prodcat;
}
}
}
- Future<Null> updateProdCategory(String oldid,String name,
- String id) async {
+ Future<Null> updateProdCategory(String id,String name) async {
try {
- String url=_getidurl+'?key="'+oldid+'"';
+ String url=_server+'/'+_db+'/'+id;
final response = await _http.get(url);
- print('Debug UPDATE GET URL: '+url);
- print('Debug UPDATE GET response: '+response.body);
var resbody=JSON.decode(response.body);
- resbody['rows'][0]['value']['name']=name;
- resbody['rows'][0]['value']['id']=id;
- final response_put = await _http.put(
- _posturl+'/'+resbody['rows'][0]['id'],
+ resbody['name']=name;
+ await _http.put(
+ _posturl+'/'+id,
headers: {'Content-Type': 'application/json'},
- body: JSON.encode(resbody['rows'][0]['value'])
+ body: JSON.encode(resbody)
);
- print('Debug UPDATE PUT response: '+response_put.body);
}
catch(e) {
throw _handleError(e);
}
}
- Future<Null> deleteProdCategory(String doc_id) async {
+ Future<Null> deleteProdCategory(String id) async {
try {
- var url=_server+'/'+_db+'/'+doc_id;
+ var url=_server+'/'+_db+'/'+id;
var response = await _http.get(url);
var reso=JSON.decode(response.body);
url=_server+'/'+_db+'/_purge/';