Implement order creation

This commit is contained in:
Rene Vergara 2021-10-27 07:59:43 -05:00
parent 064044c011
commit af8ee5dc90
8 changed files with 86 additions and 10 deletions

View file

@ -262,9 +262,10 @@ app.get('/api/order', (req, res, next) => {
if (req.query.session.length > 0) { if (req.query.session.length > 0) {
const order = ordermodel.findOne({session: req.query.session, closed: false}).then((documents) => { const order = ordermodel.findOne({session: req.query.session, closed: false}).then((documents) => {
if (documents != null) { if (documents != null) {
console.log(documents);
res.status(200).json({ res.status(200).json({
message: 'order found!', message: 'order found!',
order: documents[0] order: documents
}); });
} else { } else {
res.status(204).json({ res.status(204).json({
@ -281,4 +282,13 @@ app.get('/api/order', (req, res, next) => {
} }
}); });
app.post('/api/order', (req, res, next) => {
console.log('Post /api/order', req.body);
const order = new ordermodel(req.body.order);
order.save();
res.status(200).json({
message: 'Order added'
});
});
module.exports = app; module.exports = app;

View file

@ -6,6 +6,7 @@ import { Owner } from '../../owner.model';
import { FullnodeService } from '../../fullnode.service'; import { FullnodeService } from '../../fullnode.service';
import { UserService } from '../../user.service'; import { UserService } from '../../user.service';
import { ItemService } from '../items.service'; import { ItemService } from '../items.service';
import { OrderService} from '../../order/order.service';
import { ItemCreateComponent } from '../item-create/item-create.component'; import { ItemCreateComponent } from '../item-create/item-create.component';
import { ItemDeleteComponent } from '../item-delete/item-delete.component'; import { ItemDeleteComponent } from '../item-delete/item-delete.component';
import { ItemAddComponent } from '../item-add/item-add.component'; import { ItemAddComponent } from '../item-add/item-add.component';
@ -28,6 +29,7 @@ export class ItemListComponent implements OnInit{
constructor( constructor(
public itemService: ItemService, public itemService: ItemService,
userService: UserService, userService: UserService,
public orderService: OrderService,
public fullnodeService: FullnodeService, public fullnodeService: FullnodeService,
private dialog: MatDialog private dialog: MatDialog
) { ) {
@ -134,6 +136,7 @@ export class ItemListComponent implements OnInit{
dialogRef.afterClosed().subscribe((val) => { dialogRef.afterClosed().subscribe((val) => {
if (val != null) { if (val != null) {
console.log('Adding to order', val); console.log('Adding to order', val);
this.orderService.addToOrder(val);
} }
this.itemService.getItems(this.owner.address); this.itemService.getItems(this.owner.address);
}); });

View file

@ -30,7 +30,7 @@ export class ItemService{
} }
addItem(item: Item) { addItem(item: Item) {
const params = new HttpParams().append('item', JSON.stringify(item)); //const params = new HttpParams().append('item', JSON.stringify(item));
let obs = this.http.post<{message: string}>('http://localhost:3000/api/item', { item: item }); let obs = this.http.post<{message: string}>('http://localhost:3000/api/item', { item: item });
obs.subscribe((ItemResponse) => { obs.subscribe((ItemResponse) => {

View file

@ -0,0 +1,3 @@
.text {
font-family: 'Roboto Mono', monospace;
}

View file

@ -1 +1,13 @@
<p *ngIf="order.address.length == 0">No open order!</p> <p *ngIf="order.address.length == 0">No open order!</p>
<mat-card class="text" *ngIf="order.address.length > 0">
<table>
<tr>
<th>Qty.</th>
<th>Items</th>
</tr>
<tr *ngFor="let item of order.lines">
<td>{{item.qty}}</td>
<td>{{item.name}}</td>
</tr>
</table>
</mat-card>

View file

@ -12,7 +12,7 @@ import { OrderService } from './order.service';
}) })
export class OrderComponent implements OnInit{ export class OrderComponent implements OnInit{
public order: Order = {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, item: '', cost: 0}]}; public order: Order = {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, name: '', cost: 0}]};
public price: number = 1; public price: number = 1;
public orderUpdate: Observable<Order>; public orderUpdate: Observable<Order>;
public priceUpdate: Observable<number>; public priceUpdate: Observable<number>;
@ -23,6 +23,9 @@ export class OrderComponent implements OnInit{
) { ) {
this.priceUpdate = fullnodeService.priceUpdate; this.priceUpdate = fullnodeService.priceUpdate;
this.orderUpdate = orderService.orderUpdate; this.orderUpdate = orderService.orderUpdate;
this.orderUpdate.subscribe((order) => {
this.order = order;
});
} }
ngOnInit() { ngOnInit() {

View file

@ -1,12 +1,10 @@
import { LineItem } from '../items/lineitem.model';
export interface Order { export interface Order {
_id?: string, _id?: string,
address: string, address: string,
session: string, session: string,
timestamp: string, timestamp?: string,
closed: boolean, closed: boolean,
lines: [{ lines: LineItem[]
qty: number,
item: string,
cost: number
}]
} }

View file

@ -4,11 +4,31 @@ import { HttpClient, HttpParams } from '@angular/common/http';
import { Order } from './order.model'; import { Order } from './order.model';
import { UserService } from '../user.service'; import { UserService } from '../user.service';
import { User } from '../user.model'; import { User } from '../user.model';
import { LineItem} from '../items/lineitem.model';
@Injectable({providedIn: 'root'}) @Injectable({providedIn: 'root'})
export class OrderService { export class OrderService {
private dataStore: { user: User, order: Order } = { user:{address: '', session: '', blocktime: 0} ,order: {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, item: '' , cost:0}]} }; private dataStore: { user: User, order: Order } = {
user:{
address: '',
session: '',
blocktime: 0
},
order: {
address: '',
session: '',
timestamp: '',
closed: false,
lines: [
{
qty: 1,
name: '',
cost:0
}
]
}
};
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order); private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable(); public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
public userUpdate: Observable<User>; public userUpdate: Observable<User>;
@ -41,4 +61,31 @@ export class OrderService {
return obs; return obs;
} }
addToOrder(lineItem: LineItem) {
if (this.dataStore.order.address.length == 0) {
console.log('No open order, creating...', lineItem);
this.createOrder();
} else {
console.log('Open order, adding...', lineItem);
}
}
createOrder() {
var order:Order = {
address: this.dataStore.user.address,
session: this.dataStore.user.session,
closed: false,
lines: []
};
let obs = this.http.post<{message: string}>('http://localhost:3000/api/order', {order: order});
obs.subscribe((orderData) => {
console.log('Create order', orderData);
this.getOrder(this.dataStore.user.session);
});
return obs;
}
} }