Skip to content

Commit

Permalink
order history
Browse files Browse the repository at this point in the history
  • Loading branch information
steveww committed Feb 12, 2018
1 parent 78ca668 commit 69ebede
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 7 deletions.
7 changes: 7 additions & 0 deletions payment/payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import time
import logging
import uuid
import json
import requests
import opentracing as ot
import opentracing.ext.tags as tags
Expand Down Expand Up @@ -31,6 +32,12 @@ def pay(id):
orderid = str(uuid.uuid4())
queueOrder({ 'orderid': orderid, 'user': id, 'cart': cart })

# add to history
req = requests.post('http://user:8080/order/' + id,
data=json.dumps({'orderid': orderid, 'cart': cart}),
headers={'Content-Type': 'application/json'})
app.logger.info('order history returned {}'.format(req.status_code))

# TDOD - order history

return jsonify({ 'orderid': orderid })
Expand Down
81 changes: 75 additions & 6 deletions user/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const express = require('express');

// MongoDB
var db;
var collection;
var usersCollection;
var ordersCollection;
var mongoConnected = false;

const app = express();
Expand Down Expand Up @@ -55,7 +56,7 @@ app.get('/uniqueid', (req, res) => {
// return all users for debugging only
app.get('/users', (req, res) => {
if(mongoConnected) {
collection.find().toArray().then((users) => {
usersCollection.find().toArray().then((users) => {
res.json(users);
}).catch((e) => {
console.log('ERROR', e);
Expand All @@ -71,7 +72,7 @@ app.post('/login', (req, res) => {
if(req.body.name === undefined || req.body.password === undefined) {
res.status(400).send('name or passowrd not supplied');
} else if(mongoConnected) {
collection.findOne({
usersCollection.findOne({
name: req.body.name,
}).then((user) => {
console.log('user', user);
Expand Down Expand Up @@ -100,12 +101,12 @@ app.post('/register', (req, res) => {
res.status(400).send('insufficient data');
} else if(mongoConnected) {
// check if name already exists
collection.findOne({name: req.body.name}).then((user) => {
usersCollection.findOne({name: req.body.name}).then((user) => {
if(user) {
res.status(400).send('name already exists');
} else {
// create new user
collection.insertOne({
usersCollection.insertOne({
name: req.body.name,
password: req.body.password,
email: req.body.email
Expand All @@ -126,6 +127,73 @@ app.post('/register', (req, res) => {
}
});

app.post('/order/:id', (req, res) => {
console.log('order', req.body);
// only for registered users
if(mongoConnected) {
usersCollection.findOne({
name: req.params.id
}).then((user) => {
if(user) {
// found user record
// get orders
ordersCollection.findOne({
name: req.params.id
}).then((history) => {
if(history) {
var list = history.history;
list.push(req.body);
ordersCollection.updateOne(
{ name: req.params.id },
{ $set: { history: list }}
).then((r) => {
res.send('OK');
}).catch((e) => {
res.status(500).send(e);
});
} else {
// no history
ordersCollection.insertOne({
name: req.params.id,
history: [ req.body ]
}).then((r) => {
res.send('OK');
}).catch((e) => {
res.status(500).send(e);
});
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(404).send('name not found');
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});

app.get('/history/:id', (req, res) => {
if(mongoConnected) {
ordersCollection.findOne({
name: req.params.id
}).then((history) => {
if(history) {
res.json(history);
} else {
res.status(404).send('history not found');
}
}).catch((e) => {
res.status(500).send(e);
});
} else {
res.status(500).send('database not available');
}
});

// connect to Redis
var redisClient = redis.createClient({
host: 'redis'
Expand All @@ -147,7 +215,8 @@ function mongoConnect() {
reject(error);
} else {
db = _db;
collection = db.collection('users');
usersCollection = db.collection('users');
ordersCollection = db.collection('orders');
resolve('connected');
}
});
Expand Down
14 changes: 14 additions & 0 deletions web/static/js/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@
// 404 is OK as cart might not exist yet
console.log('ERROR', e);
});
loadHistory(currentUser.user.name);
}).catch((e) => {
console.log('ERROR', e);
$scope.data.message = 'ERROR ' + e.data;
Expand Down Expand Up @@ -485,9 +486,22 @@
});
};

function loadHistory(id) {
$http({
url: '/api/user/history/' + id,
method: 'GET'
}).then((res) => {
console.log('history', res.data);
$scope.data.orderHistory = res.data.history;
}).catch((e) => {
console.log('ERROR', e);
});
}

console.log('loginform init');
if(!angular.equals(currentUser.user, {})) {
$scope.data.user = currentUser.user;
loadHistory(currentUser.user.name);
}
});

Expand Down
19 changes: 18 additions & 1 deletion web/static/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,23 @@
<h3>Greetings {{ data.user.name }}</h3>
<p>Email - {{ data.user.email }}</p>
<h3>Order History</h3>
<p>Order history will be here</p>
<table>
<tr>
<th>Order ID</th>
<th>Items</th>
<th>Total</th>
</tr>
<tr ng-repeat="hist in data.orderHistory">
<td>{{ hist.orderid }}</td>
<td>
<ul>
<li ng-repeat="item in hist.cart.items">{{ item.name }}</li>
</ul>
</td>
<td>
&euro;{{ hist.cart.total }}
</td>
</tr>
</table>
</div>
</div>

0 comments on commit 69ebede

Please sign in to comment.