import 'product_component.dart';
import 'product_category_component.dart';
import 'product_detail_component.dart';
-import 'product_category_detail_component.dart';
import 'product_service.dart';
import 'product_category_service.dart';
path: '/product/:id',
name: 'ProductDetail',
component: ProductDetailComponent
- ),
- const Route(
- path: '/product_category/:id',
- name: 'ProductCategoryDetail',
- component: ProductCategoryDetailComponent
)
])
class ProductCategory {
- String doc_id;
- String name;
String id;
- String doc_type;
+ String name;
+ String type;
- ProductCategory(this.doc_id,this.name,this.id,this.doc_type);
+ ProductCategory(this.id,this.name,this.type);
}
class ProductCategoryComponent implements OnInit {
List<ProductCategory> product_categories;
ProductCategory selected_prod_category;
- String new_prod_category_name;
- String new_prod_category_id;
+ String new_prod_category_name='';
final ProductCategoryService _prodcatSrv;
final Router _router;
- var docreate=false;
ProductCategoryComponent(this._prodcatSrv,this._router);
- Future<Null> getProductCategories() async {
- product_categories = await _prodcatSrv.getAll();
- }
-
Future<Null> createProductCategory() async {
- await _prodcatSrv.createProdCategory(
- new_prod_category_name,
- new_prod_category_id
- );
+ await _prodcatSrv.createProdCategory(new_prod_category_name);
+ await ngOnInit();
}
- void ngOnInit() {
- getProductCategories();
- }
-
- void checkInput() {
- if(new_prod_category_name=='' || new_prod_category_id=='') {
- docreate=false;
- }
- else {
- docreate=true;
- for(var cat in product_categories) {
- if(cat.id==new_prod_category_id) {
- docreate=false;
- break;
- }
- }
- }
+ Future<Null> ngOnInit() async {
+ product_categories = await _prodcatSrv.getAll();
}
choose(ProductCategory pt) {
<div class="mdl-textfield mdl-js-textfield
mdl-textfield--floating-label ain">
<input class="mdl-textfield__input" type=text id=ptn
- [(ngModel)]="new_prod_category_name"
- (keyup)="checkInput()">
+ [(ngModel)]="new_prod_category_name">
<label class="mdl-textfield__label" for=ptn>Name</label>
</div>
- <div class="mdl-textfield mdl-js-textfield
- mdl-textfield--floating-label ain">
- <input class="mdl-textfield__input" type=text id=pti
- [(ngModel)]="new_prod_category_id"
- (keyup)="checkInput()">
- <label class="mdl-textfield__label" for=pti>Identifier</label>
- </div>
<button class="mdl-button mdl-js-button mdl-button--raised abtn"
(click)="createProductCategory()"
- [disabled]="!docreate">
+ [disabled]="new_prod_category_name==''">
Create
</button>
</div>
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<Null> createProdCategory(String name) async {
try {
await _http.post(
_posturl,
headers: {'Content-Type': 'application/json'},
body: JSON.encode({
'name': name,
- 'id': id,
'type': 'product_category'
})
);
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/';
final RouteParams _routeParams;
final Router _router;
- String prod_category_id;
+ String catid;
String prod_category_name;
+
String prod_name;
double prod_price;
int prodcnt;
ProductComponent(this._prodSrv,this._prodcatSrv,
this._routeParams,this._router) {
prod_category_name='Category';
- prod_category_id='ID';
}
Future<Null> ngOnInit() async {
- var catid=_routeParams.get('id');
+ catid=_routeParams.get('id');
if(catid!=null) {
products = await (_prodSrv.getByCategory(catid));
prodcat = await (_prodcatSrv.getById(catid));
}
prod_category_name=prodcat.name;
- prod_category_id=prodcat.id;
prodcnt=products.length;
}
Future<Null> updateProductCategory() async {
- bool doupdate=false;
- bool changecat=false;
- if(prodcat.id!=prod_category_id) {
- doupdate=true;
- changecat=true;
- }
if(prodcat.name!=prod_category_name) {
- doupdate=true;
- }
- if(doupdate) {
- if(changecat) {
- for(Product prod in products) {
- print('Debug: change category of '+
- prod.name);
- await _prodSrv.updateProd(
- prod.id,
- prod.name,
- prod.price,
- prod_category_id
- );
- }
- }
- print('Debug: Updating product category '+
- prodcat.name+'/'+prodcat.id+' -> '+
- prod_category_name+'/'+prod_category_id);
await _prodcatSrv.updateProdCategory(
- prodcat.id,
- prod_category_name,
- prod_category_id
+ catid,
+ prod_category_name
);
+ _router.navigate(['ProductCategories']);
}
}
Future<Null> deleteProductCategory() async {
- bool delete=false;
- if(products.length==0) {
- delete=true;
- }
- else {
- print('Debug: Not deleting anything!');
- }
- if(delete) {
- print('Debug: Deleting product category '+
- prodcat.name+'/'+prodcat.id);
- await _prodcatSrv.deleteProdCategory(prodcat.doc_id);
- }
+ print('Debug: Deleting product category '+
+ prodcat.name+'/'+prodcat.id);
+ await _prodcatSrv.deleteProdCategory(prodcat.id);
+ _router.navigate(['ProductCategories']);
}
Future<Null> createProduct() async {
return;
print('Debug: Creating product '+prod_name+'/'+
prod_price.toString());
- await _prodSrv.createProduct(prod_name,
+ String id = await _prodSrv.createProduct(prod_name,
double.parse(prod_price),
- prod_category_id);
+ catid);
+ // instead of executing ngOnInit to 'reload' content
+ products.add(new Product(
+ id,prod_name,double.parse(prod_price),'product',catid
+ ));
+ prod_name='';
+ prod_price='';
}
choose(Product prod) {
[(ngModel)]="prod_category_name">
<label class="mdl-textfield__label" for="ptn">Name</label>
</div>
- <div class="mdl-textfield mdl-js-textfield
- mdl-textfield--floating-label ain">
- <input class="mdl-textfield__input" type="text" id="pti"
- [(ngModel)]="prod_category_id">
- <label class="mdl-textfield__label" for="pti">
- Identifier (arbitrary but unique)
- </label>
- </div>
<button class="mdl-button mdl-js-button mdl-button--raised abtn"
- (click)="updateProductCategory()">
+ (click)="updateProductCategory()"
+ [disabled]="prod_category_name==''">
Update
</button>
<button class="mdl-button mdl-js-button mdl-button--raised abtn"
margin-left: 5%;
}
+.aseldesc {
+ font-size: 12px;
+ margin-bottom: 10px;
+}
</div>
</div>
<div class=asel>
- Product category<br><br>
+ <div class=aseldesc>
+ Product category
+ </div>
<select [(ngModel)]="prod_category" required>
<option *ngFor="let prodcat of prodcats"
[value]="prodcat.id">
Future<Null> createProduct(String name,double price,String cat) async {
try {
var url=_server+'/'+_db;
- await _http.post(
+ var response = await _http.post(
url,
headers: {'Content-Type': 'application/json'},
body: JSON.encode({
'category': cat
})
);
+ return(JSON.decode(response.body)['id']);
}
catch(e) {
throw _handleError(e);