From af8ee5dc908a1cd15faeddac0509a35e56d1567e Mon Sep 17 00:00:00 2001 From: Rene Vergara Date: Wed, 27 Oct 2021 07:59:43 -0500 Subject: [PATCH] Implement order creation --- backend/app.js | 12 ++++- .../items/item-list/item-list.component.ts | 3 ++ src/app/items/items.service.ts | 2 +- src/app/order/order.component.css | 3 ++ src/app/order/order.component.html | 12 +++++ src/app/order/order.component.ts | 5 +- src/app/order/order.model.ts | 10 ++-- src/app/order/order.service.ts | 49 ++++++++++++++++++- 8 files changed, 86 insertions(+), 10 deletions(-) diff --git a/backend/app.js b/backend/app.js index 22a075d..c3a89a4 100644 --- a/backend/app.js +++ b/backend/app.js @@ -262,9 +262,10 @@ app.get('/api/order', (req, res, next) => { if (req.query.session.length > 0) { const order = ordermodel.findOne({session: req.query.session, closed: false}).then((documents) => { if (documents != null) { + console.log(documents); res.status(200).json({ message: 'order found!', - order: documents[0] + order: documents }); } else { 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; diff --git a/src/app/items/item-list/item-list.component.ts b/src/app/items/item-list/item-list.component.ts index acd567f..89b28c1 100644 --- a/src/app/items/item-list/item-list.component.ts +++ b/src/app/items/item-list/item-list.component.ts @@ -6,6 +6,7 @@ import { Owner } from '../../owner.model'; import { FullnodeService } from '../../fullnode.service'; import { UserService } from '../../user.service'; import { ItemService } from '../items.service'; +import { OrderService} from '../../order/order.service'; import { ItemCreateComponent } from '../item-create/item-create.component'; import { ItemDeleteComponent } from '../item-delete/item-delete.component'; import { ItemAddComponent } from '../item-add/item-add.component'; @@ -28,6 +29,7 @@ export class ItemListComponent implements OnInit{ constructor( public itemService: ItemService, userService: UserService, + public orderService: OrderService, public fullnodeService: FullnodeService, private dialog: MatDialog ) { @@ -134,6 +136,7 @@ export class ItemListComponent implements OnInit{ dialogRef.afterClosed().subscribe((val) => { if (val != null) { console.log('Adding to order', val); + this.orderService.addToOrder(val); } this.itemService.getItems(this.owner.address); }); diff --git a/src/app/items/items.service.ts b/src/app/items/items.service.ts index 744cdea..c39dd40 100644 --- a/src/app/items/items.service.ts +++ b/src/app/items/items.service.ts @@ -30,7 +30,7 @@ export class ItemService{ } 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 }); obs.subscribe((ItemResponse) => { diff --git a/src/app/order/order.component.css b/src/app/order/order.component.css index e69de29..57c4236 100644 --- a/src/app/order/order.component.css +++ b/src/app/order/order.component.css @@ -0,0 +1,3 @@ +.text { + font-family: 'Roboto Mono', monospace; +} diff --git a/src/app/order/order.component.html b/src/app/order/order.component.html index d149f9d..44ebe1e 100644 --- a/src/app/order/order.component.html +++ b/src/app/order/order.component.html @@ -1 +1,13 @@

No open order!

+ + + + + + + + + + +
Qty.Items
{{item.qty}}{{item.name}}
+
diff --git a/src/app/order/order.component.ts b/src/app/order/order.component.ts index f29e2af..4fedf5f 100644 --- a/src/app/order/order.component.ts +++ b/src/app/order/order.component.ts @@ -12,7 +12,7 @@ import { OrderService } from './order.service'; }) 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 orderUpdate: Observable; public priceUpdate: Observable; @@ -23,6 +23,9 @@ export class OrderComponent implements OnInit{ ) { this.priceUpdate = fullnodeService.priceUpdate; this.orderUpdate = orderService.orderUpdate; + this.orderUpdate.subscribe((order) => { + this.order = order; + }); } ngOnInit() { diff --git a/src/app/order/order.model.ts b/src/app/order/order.model.ts index fc380a4..0dd0991 100644 --- a/src/app/order/order.model.ts +++ b/src/app/order/order.model.ts @@ -1,12 +1,10 @@ +import { LineItem } from '../items/lineitem.model'; + export interface Order { _id?: string, address: string, session: string, - timestamp: string, + timestamp?: string, closed: boolean, - lines: [{ - qty: number, - item: string, - cost: number - }] + lines: LineItem[] } diff --git a/src/app/order/order.service.ts b/src/app/order/order.service.ts index d8df8e6..799e679 100644 --- a/src/app/order/order.service.ts +++ b/src/app/order/order.service.ts @@ -4,11 +4,31 @@ import { HttpClient, HttpParams } from '@angular/common/http'; import { Order } from './order.model'; import { UserService } from '../user.service'; import { User } from '../user.model'; +import { LineItem} from '../items/lineitem.model'; @Injectable({providedIn: 'root'}) 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 = new BehaviorSubject(this.dataStore.order); public readonly orderUpdate: Observable = this._orderUpdated.asObservable(); public userUpdate: Observable; @@ -41,4 +61,31 @@ export class OrderService { 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; + + } + }