Commit fc16b503 authored by Jasper Verbeet's avatar Jasper Verbeet 🙇🏻

More endpoints

parent 0a0bc6c3
{"a":"b","b":{"test":true}}
\ No newline at end of file
{"status":false,"origin":"Eindhoven","destination":"Helsinki","width":233,"height":300,"depth":300,"weight":300,"new":false}
\ No newline at end of file
......@@ -47,17 +47,17 @@ const getDb = () => {
const getDbItem = (key) => {
const db = getDb();
const results = Object.entries(db)[0].filter(
(k, _) => k == key);
const results = Object.keys(db).filter(
(k) => k == key);
if (results.length > 0) {
if (results) {
return db[results[0]];
}
return null;
}
const setDbItem = (key, value, overwrite=false) => {
const setDbItem = (key, value, overwrite=true) => {
let result = null
if (!overwrite) {
result = getDbItem(key);
......@@ -73,22 +73,92 @@ const setDbItem = (key, value, overwrite=false) => {
return false;
}
app.get('/api/notification', (req, res) => {
const newItem = getDbItem('new');
if (newItem) {
setDbItem('new', false);
const response = {
origin: getDbItem('origin'),
destination: getDbItem('destination'),
width: getDbItem('width'),
height: getDbItem('height'),
depth: getDbItem('depth'),
weight: getDbItem('weight'),
}
console.log(colors.yellow(`New notification shown to user`));
return res.json({result: response});
}
res.json({result: false});
});
app.get('/api/status', (req, res) => {
res.json({result: getDbItem('status')});
});
app.post('/api/shipment', (req, res) => {
const expected = ['origin', 'destination', 'width', 'height', 'depth', 'weight'];
const exact = expected.filter((val) => !(val in req.body));
if (exact.length != 0) {
res.status = 400;
return res.json({result: 'error'});
}
setDbItem('status', 0);
setDbItem('new', true);
for (let val of expected) {
setDbItem(val, req.body[val]);
}
console.log(colors.green(`New shipment from ${req.body.origin} to ${req.body.destination}`));
app.get('/api/wait', (req, res) => {
let count = 0;
console.log(colors.green('Waiting...'));
while (count < 4500000000) {
count++;
setTimeout(() => {
if (getDbItem('status') == 0) {
setDbItem('status', false);
setDbItem('new', false);
console.log(colors.red(`Timed out, declining...`));
}
}, 5000)
res.json({result: 'ok'});
});
app.post('/api/accept', (req, res) => {
const state = req.body['state'];
if (state == null) {
res.status = 400;
return res.json({result: 'supply a `state` in the json body'});
}
console.log(colors.green('Done'));
res.json({
result: Math.random() < .5 ? true : false,
});
if (getDbItem('status') === 0) {
setDbItem('status', state);
console.log(colors.green(`Updated status: ${state}`));
}
else {
console.log(colors.yellow(`Status was already set.`));
return res.json({result: 'Status was already set.'});
}
res.json({result: 'ok'});
});
app.post('/api/b', (req, res) => {
app.post('/api/details', (req, res) => {
const expected = ['total_weight', 'current_weight', 'long', 'lat',];
const exact = expected.filter((val) => !(val in req.body));
if (exact.length != 0) {
res.status = 400;
return res.json({result: 'error'});
}
setDbItem('total_weight', res.body.total_weight);
setDbItem('long', res.body.long);
setDbItem('lat', res.body.lat);
res.json({result: 'ok'});
});
// START THE SERVER
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment