Implemented basic order service
Implemented adding the component to the viewer and the search in database. Fixed bug in search of item list
This commit is contained in:
parent
eea8e06d48
commit
172ebb0171
13 changed files with 146 additions and 21 deletions
|
@ -5,6 +5,7 @@ const postmodel = require('./models/post');
|
||||||
const usermodel = require('./models/user');
|
const usermodel = require('./models/user');
|
||||||
const ownermodel = require('./models/owner');
|
const ownermodel = require('./models/owner');
|
||||||
const itemmodel = require('./models/item');
|
const itemmodel = require('./models/item');
|
||||||
|
const ordermodel = require('./models/order');
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
const stdrpc = require('stdrpc');
|
const stdrpc = require('stdrpc');
|
||||||
const CoinGecko = require('coingecko-api');
|
const CoinGecko = require('coingecko-api');
|
||||||
|
@ -181,20 +182,28 @@ app.post('/api/addownername', (req, res, next) => {
|
||||||
|
|
||||||
app.get('/api/getitems', (req, res, next) => {
|
app.get('/api/getitems', (req, res, next) => {
|
||||||
console.log('Get: /api/getitems');
|
console.log('Get: /api/getitems');
|
||||||
const items = itemmodel.find({address: req.body.address}).then((documents) => {
|
//console.log('getitems', req.query.address);
|
||||||
if(documents.length > 0){
|
if (req.query.address.length > 0 ) {
|
||||||
//console.log(documents);
|
const items = itemmodel.find({user: req.query.address}).then((documents) => {
|
||||||
res.status(200).json({
|
if(documents.length > 0){
|
||||||
message: 'items found!',
|
//console.log(documents);
|
||||||
items: documents
|
res.status(200).json({
|
||||||
});
|
message: 'items found!',
|
||||||
} else {
|
items: documents
|
||||||
res.status(204).json({
|
});
|
||||||
message: 'items not found!',
|
} else {
|
||||||
items: []
|
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) => {
|
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;
|
module.exports = app;
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
const mongoose = require('mongoose');
|
const mongoose = require('mongoose');
|
||||||
|
|
||||||
const userSchema = mongoose.Schema({
|
const itemSchema = mongoose.Schema({
|
||||||
name: {type: String, required: true},
|
name: {type: String, required: true},
|
||||||
description: {type: String, required: true},
|
description: {type: String, required: true},
|
||||||
user: {type: String, required: true},
|
user: {type: String, required: true},
|
||||||
cost: {type: Number, required: true}
|
cost: {type: Number, required: true}
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = mongoose.model('Item', userSchema);
|
module.exports = mongoose.model('Item', itemSchema);
|
||||||
|
|
|
@ -2,6 +2,7 @@ const mongoose = require('mongoose');
|
||||||
|
|
||||||
const orderSchema = mongoose.Schema({
|
const orderSchema = mongoose.Schema({
|
||||||
address: {type: String, required: true},
|
address: {type: String, required: true},
|
||||||
|
session: {type: String, required: true},
|
||||||
timestamp: {type: Date, required: true, default: Date.now},
|
timestamp: {type: Date, required: true, default: Date.now},
|
||||||
closed: { type: Boolean, required: true, default:false },
|
closed: { type: Boolean, required: true, default:false },
|
||||||
lines: [{
|
lines: [{
|
||||||
|
@ -10,3 +11,5 @@ const orderSchema = mongoose.Schema({
|
||||||
cost: { type: Number, required: true, default: 0}
|
cost: { type: Number, required: true, default: 0}
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
|
module.exports = mongoose.model('Order', orderSchema);
|
||||||
|
|
|
@ -16,6 +16,7 @@ import { ViewerComponent } from './viewer/viewer.component';
|
||||||
import { LoginComponent } from './login/login.component';
|
import { LoginComponent } from './login/login.component';
|
||||||
import { ItemListComponent } from './items/item-list/item-list.component';
|
import { ItemListComponent } from './items/item-list/item-list.component';
|
||||||
import { ItemCreateComponent } from './items/item-create/item-create.component';
|
import { ItemCreateComponent } from './items/item-create/item-create.component';
|
||||||
|
import { OrderComponent } from './order/order.component';
|
||||||
//import { NameDialogComponent } from './namedialog/namedialog.component';
|
//import { NameDialogComponent } from './namedialog/namedialog.component';
|
||||||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
|
|
||||||
|
@ -26,6 +27,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||||
ViewerComponent,
|
ViewerComponent,
|
||||||
ItemListComponent,
|
ItemListComponent,
|
||||||
LoginComponent,
|
LoginComponent,
|
||||||
|
OrderComponent,
|
||||||
ItemCreateComponent
|
ItemCreateComponent
|
||||||
//NameDialogComponent,
|
//NameDialogComponent,
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<h1>Items!</h1>
|
<h1>Items!</h1>
|
||||||
<div *ngIf="items.length > 0">
|
<div *ngIf="items.length > 0">
|
||||||
<div class="card" *ngFor="let item of items">
|
<div class="card" *ngFor="let item of itemsUpdate | async">
|
||||||
<mat-card>
|
<mat-card>
|
||||||
<mat-card-title>
|
<mat-card-title>
|
||||||
<table cellspacing="0" width="100%">
|
<table cellspacing="0" width="100%">
|
||||||
|
|
|
@ -34,6 +34,7 @@ export class ItemListComponent implements OnInit{
|
||||||
this.priceUpdate = fullnodeService.priceUpdate;
|
this.priceUpdate = fullnodeService.priceUpdate;
|
||||||
this.ownerUpdate.subscribe((owner) => {
|
this.ownerUpdate.subscribe((owner) => {
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
|
console.log('owner address', this.owner.address);
|
||||||
itemService.getItems(this.owner.address);
|
itemService.getItems(this.owner.address);
|
||||||
this.itemsUpdate.subscribe((items) => {
|
this.itemsUpdate.subscribe((items) => {
|
||||||
this.items = items;
|
this.items = items;
|
||||||
|
|
0
src/app/order/order.component.css
Normal file
0
src/app/order/order.component.css
Normal file
1
src/app/order/order.component.html
Normal file
1
src/app/order/order.component.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
<p *ngIf="order.address.length == 0">No open order!</p>
|
30
src/app/order/order.component.ts
Normal file
30
src/app/order/order.component.ts
Normal file
|
@ -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<Order>;
|
||||||
|
public priceUpdate: Observable<number>;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
public fullnodeService: FullnodeService,
|
||||||
|
public orderService: OrderService
|
||||||
|
) {
|
||||||
|
this.priceUpdate = fullnodeService.priceUpdate;
|
||||||
|
this.orderUpdate = orderService.orderUpdate;
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
}
|
||||||
|
}
|
12
src/app/order/order.model.ts
Normal file
12
src/app/order/order.model.ts
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
export interface Order {
|
||||||
|
_id?: string,
|
||||||
|
address: string,
|
||||||
|
session: string,
|
||||||
|
timestamp: string,
|
||||||
|
closed: boolean,
|
||||||
|
lines: [{
|
||||||
|
qty: number,
|
||||||
|
item: string,
|
||||||
|
cost: number
|
||||||
|
}]
|
||||||
|
}
|
|
@ -1,13 +1,44 @@
|
||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { Subject, BehaviorSubject, Observable } from 'rxjs';
|
import { Subject, BehaviorSubject, Observable } from 'rxjs';
|
||||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
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'})
|
@Injectable({providedIn: 'root'})
|
||||||
|
|
||||||
export class OrderService {
|
export class OrderService {
|
||||||
//TODO implement the Order interface
|
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: { order: string } = { order: ''};
|
private _orderUpdated: BehaviorSubject<Order> = new BehaviorSubject(this.dataStore.order);
|
||||||
|
public readonly orderUpdate: Observable<Order> = this._orderUpdated.asObservable();
|
||||||
|
public userUpdate: Observable<User>;
|
||||||
|
|
||||||
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,10 +23,12 @@ export class UserService{
|
||||||
private uName = '';
|
private uName = '';
|
||||||
private session: string | null = '';
|
private session: string | null = '';
|
||||||
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
|
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
|
||||||
|
private _userUpdated: BehaviorSubject<User> = new BehaviorSubject(this.dataStore.user);
|
||||||
private uNameUpdated = new Subject<string>();
|
private uNameUpdated = new Subject<string>();
|
||||||
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
|
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
|
||||||
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
|
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
|
||||||
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
|
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
|
||||||
|
public readonly userUpdate: Observable<User> = this._userUpdated.asObservable();
|
||||||
|
|
||||||
constructor(private http: HttpClient){
|
constructor(private http: HttpClient){
|
||||||
this.session = localStorage.getItem('s4z_token');
|
this.session = localStorage.getItem('s4z_token');
|
||||||
|
@ -45,6 +47,7 @@ export class UserService{
|
||||||
this.dataStore.user = UserDataResponse.body!.user[0];
|
this.dataStore.user = UserDataResponse.body!.user[0];
|
||||||
console.log(`US: Found user, returning it`);
|
console.log(`US: Found user, returning it`);
|
||||||
this._uZaddrUpdated.next(Object.assign({},this.dataStore).user.address);
|
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);
|
this.getOwner(Object.assign({},this.dataStore.user).address);
|
||||||
} else {
|
} else {
|
||||||
console.log('US: Did not find user');
|
console.log('US: Did not find user');
|
||||||
|
|
|
@ -2,4 +2,13 @@
|
||||||
<div align="center">
|
<div align="center">
|
||||||
<h1>{{message}}</h1>
|
<h1>{{message}}</h1>
|
||||||
</div>
|
</div>
|
||||||
<app-item-list></app-item-list>
|
<table cellspacing="0" width="100%">
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
<app-item-list></app-item-list>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<app-order></app-order>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
Loading…
Reference in a new issue