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:
Rene Vergara 2021-10-25 15:01:06 -05:00
parent eea8e06d48
commit 172ebb0171
13 changed files with 146 additions and 21 deletions

View file

@ -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;

View file

@ -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);

View file

@ -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);

View file

@ -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,
],

View file

@ -1,6 +1,6 @@
<h1>Items!</h1>
<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-title>
<table cellspacing="0" width="100%">

View file

@ -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;

View file

View file

@ -0,0 +1 @@
<p *ngIf="order.address.length == 0">No open order!</p>

View 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() {
}
}

View 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
}]
}

View file

@ -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<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;
}
}

View file

@ -23,10 +23,12 @@ export class UserService{
private uName = '';
private session: string | null = '';
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
private _userUpdated: BehaviorSubject<User> = new BehaviorSubject(this.dataStore.user);
private uNameUpdated = new Subject<string>();
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
public readonly userUpdate: Observable<User> = 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');

View file

@ -2,4 +2,13 @@
<div align="center">
<h1>{{message}}</h1>
</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>