diff --git a/backend/app.js b/backend/app.js index b4e573c..d9f10af 100644 --- a/backend/app.js +++ b/backend/app.js @@ -5,6 +5,7 @@ const postmodel = require('./models/post'); const usermodel = require('./models/user'); const ownermodel = require('./models/owner'); const itemmodel = require('./models/item'); +const ordermodel = require('./models/order'); const mongoose = require('mongoose'); const stdrpc = require('stdrpc'); const CoinGecko = require('coingecko-api'); @@ -181,20 +182,28 @@ app.post('/api/addownername', (req, res, next) => { app.get('/api/getitems', (req, res, next) => { console.log('Get: /api/getitems'); - const items = itemmodel.find({address: req.body.address}).then((documents) => { - if(documents.length > 0){ - //console.log(documents); - res.status(200).json({ - message: 'items found!', - items: documents - }); - } else { - res.status(204).json({ - message: 'items not found!', - items: [] - }); - } - }); + //console.log('getitems', req.query.address); + if (req.query.address.length > 0 ) { + const items = itemmodel.find({user: req.query.address}).then((documents) => { + if(documents.length > 0){ + //console.log(documents); + res.status(200).json({ + message: 'items found!', + items: documents + }); + } else { + res.status(204).json({ + message: 'items not found!', + items: [] + }); + } + }); + } else { + res.status(204).json({ + message: 'no address', + items: [] + }); + } }); app.post('/api/item', (req, res, next) => { @@ -221,4 +230,28 @@ app.get('/api/price', (req, res, next) => { }); }); +app.get('/api/order', (req, res, next) => { + console.log('Get /api/order'); + if (req.query.session.length > 0) { + const order = ordermodel.findOne({session: req.query.session, closed: false}).then((documents) => { + if (documents != null) { + res.status(200).json({ + message: 'order found!', + order: documents[0] + }); + } else { + res.status(204).json({ + message: 'no order found!', + order: null + }); + } + }); + } else { + res.status(204).json({ + message: 'no session received', + order: null + }); + } +}); + module.exports = app; diff --git a/backend/models/item.js b/backend/models/item.js index 2333d22..d45aa7d 100644 --- a/backend/models/item.js +++ b/backend/models/item.js @@ -1,10 +1,10 @@ const mongoose = require('mongoose'); -const userSchema = mongoose.Schema({ +const itemSchema = mongoose.Schema({ name: {type: String, required: true}, description: {type: String, required: true}, user: {type: String, required: true}, cost: {type: Number, required: true} }); -module.exports = mongoose.model('Item', userSchema); +module.exports = mongoose.model('Item', itemSchema); diff --git a/backend/models/order.js b/backend/models/order.js index bfdf2b9..64af430 100644 --- a/backend/models/order.js +++ b/backend/models/order.js @@ -2,6 +2,7 @@ const mongoose = require('mongoose'); const orderSchema = mongoose.Schema({ address: {type: String, required: true}, + session: {type: String, required: true}, timestamp: {type: Date, required: true, default: Date.now}, closed: { type: Boolean, required: true, default:false }, lines: [{ @@ -10,3 +11,5 @@ const orderSchema = mongoose.Schema({ cost: { type: Number, required: true, default: 0} }] }); + +module.exports = mongoose.model('Order', orderSchema); diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 5dea57f..8292c5e 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -16,6 +16,7 @@ import { ViewerComponent } from './viewer/viewer.component'; import { LoginComponent } from './login/login.component'; import { ItemListComponent } from './items/item-list/item-list.component'; import { ItemCreateComponent } from './items/item-create/item-create.component'; +import { OrderComponent } from './order/order.component'; //import { NameDialogComponent } from './namedialog/namedialog.component'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; @@ -26,6 +27,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; ViewerComponent, ItemListComponent, LoginComponent, + OrderComponent, ItemCreateComponent //NameDialogComponent, ], diff --git a/src/app/items/item-list/item-list.component.html b/src/app/items/item-list/item-list.component.html index 4d0d729..e004b65 100644 --- a/src/app/items/item-list/item-list.component.html +++ b/src/app/items/item-list/item-list.component.html @@ -1,6 +1,6 @@

Items!

-
+
diff --git a/src/app/items/item-list/item-list.component.ts b/src/app/items/item-list/item-list.component.ts index 89b4332..c00b6f7 100644 --- a/src/app/items/item-list/item-list.component.ts +++ b/src/app/items/item-list/item-list.component.ts @@ -34,6 +34,7 @@ export class ItemListComponent implements OnInit{ this.priceUpdate = fullnodeService.priceUpdate; this.ownerUpdate.subscribe((owner) => { this.owner = owner; + console.log('owner address', this.owner.address); itemService.getItems(this.owner.address); this.itemsUpdate.subscribe((items) => { this.items = items; diff --git a/src/app/order/order.component.css b/src/app/order/order.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/order/order.component.html b/src/app/order/order.component.html new file mode 100644 index 0000000..d149f9d --- /dev/null +++ b/src/app/order/order.component.html @@ -0,0 +1 @@ +

No open order!

diff --git a/src/app/order/order.component.ts b/src/app/order/order.component.ts new file mode 100644 index 0000000..f29e2af --- /dev/null +++ b/src/app/order/order.component.ts @@ -0,0 +1,30 @@ +import { Component, OnInit } from '@angular/core'; +import { Observable } from 'rxjs'; +import { Order } from './order.model'; +import { FullnodeService } from '../fullnode.service'; +import { UserService } from '../user.service'; +import { OrderService } from './order.service'; + +@Component({ + selector: 'app-order', + templateUrl: './order.component.html', + styleUrls: ['./order.component.css'] +}) + +export class OrderComponent implements OnInit{ + public order: Order = {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, item: '', cost: 0}]}; + public price: number = 1; + public orderUpdate: Observable; + public priceUpdate: Observable; + + constructor( + public fullnodeService: FullnodeService, + public orderService: OrderService + ) { + this.priceUpdate = fullnodeService.priceUpdate; + this.orderUpdate = orderService.orderUpdate; + } + + ngOnInit() { + } +} diff --git a/src/app/order/order.model.ts b/src/app/order/order.model.ts new file mode 100644 index 0000000..fc380a4 --- /dev/null +++ b/src/app/order/order.model.ts @@ -0,0 +1,12 @@ +export interface Order { + _id?: string, + address: string, + session: string, + timestamp: string, + closed: boolean, + lines: [{ + qty: number, + item: string, + cost: number + }] +} diff --git a/src/app/order/order.service.ts b/src/app/order/order.service.ts index 5fc2422..d8df8e6 100644 --- a/src/app/order/order.service.ts +++ b/src/app/order/order.service.ts @@ -1,13 +1,44 @@ import { Injectable } from '@angular/core'; import { Subject, BehaviorSubject, Observable } from 'rxjs'; import { HttpClient, HttpParams } from '@angular/common/http'; +import { Order } from './order.model'; +import { UserService } from '../user.service'; +import { User } from '../user.model'; @Injectable({providedIn: 'root'}) export class OrderService { - //TODO implement the Order interface - private dataStore: { order: string } = { order: ''}; + private dataStore: { user: User, order: Order } = { user:{address: '', session: '', blocktime: 0} ,order: {address: '', session: '', timestamp: '', closed: false, lines: [{qty: 1, item: '' , cost:0}]} }; + private _orderUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.order); + public readonly orderUpdate: Observable = this._orderUpdated.asObservable(); + public userUpdate: Observable; - constructor(private http: HttpClient) { + constructor( + private http: HttpClient, + public userService: UserService + ) { + this.userUpdate = userService.userUpdate; + this.userUpdate.subscribe((user) => { + this.dataStore.user = user; + //console.log('OS: const', user); + this.getOrder(this.dataStore.user.session); + }); } + + getOrder(session: string) { + const params = new HttpParams().append('session', session); + let obs = this.http.get<{message: string, order: any}>('http://localhost:3000/api/order', { headers:{}, params:params, observe: 'response'}); + + obs.subscribe((OrderDataResponse) => { + if (OrderDataResponse.status == 200) { + this.dataStore.order = OrderDataResponse.body!.order; + this._orderUpdated.next(Object.assign({}, this.dataStore).order); + } else { + console.log('No order found'); + } + }); + + return obs; + } + } diff --git a/src/app/user.service.ts b/src/app/user.service.ts index bfb8414..5b15492 100644 --- a/src/app/user.service.ts +++ b/src/app/user.service.ts @@ -23,10 +23,12 @@ export class UserService{ private uName = ''; private session: string | null = ''; private _uZaddrUpdated: BehaviorSubject = new BehaviorSubject(this.uZaddr); + private _userUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.user); private uNameUpdated = new Subject(); private _ownerUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.owner); public readonly uZaddrUpdate: Observable = this._uZaddrUpdated.asObservable(); public readonly ownerUpdate: Observable = this._ownerUpdated.asObservable(); + public readonly userUpdate: Observable = this._userUpdated.asObservable(); constructor(private http: HttpClient){ this.session = localStorage.getItem('s4z_token'); @@ -45,6 +47,7 @@ export class UserService{ this.dataStore.user = UserDataResponse.body!.user[0]; console.log(`US: Found user, returning it`); this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address); + this._userUpdated.next(Object.assign({}, this.dataStore).user); this.getOwner(Object.assign({},this.dataStore.user).address); } else { console.log('US: Did not find user'); diff --git a/src/app/viewer/viewer.component.html b/src/app/viewer/viewer.component.html index 8c79dc0..09706f2 100644 --- a/src/app/viewer/viewer.component.html +++ b/src/app/viewer/viewer.component.html @@ -2,4 +2,13 @@

{{message}}

- +
+ + + + +
+ + + +