WME/aufgabe3/app.js

153 lines
4 KiB
JavaScript
Raw Normal View History

2018-12-06 11:21:40 +00:00
// DO NOT CHANGE!
//init app with express, util, body-parser, csv2json
2019-01-05 13:43:01 +00:00
const express = require('express');
const app = express()
const path = require('path');
const bodyParser = require('body-parser');
2018-12-06 11:21:40 +00:00
//register body-parser to handle json from res / req
2018-12-06 11:52:54 +00:00
app.use(bodyParser.json());
2018-12-06 11:21:40 +00:00
//register public dir to serve static files (html, css, js)
2018-12-06 11:52:54 +00:00
app.use(express.static(path.join(__dirname, "public")));
2018-12-06 11:21:40 +00:00
// END DO NOT CHANGE!
/**************************************************************************
****************************** csv2json *********************************
**************************************************************************/
2019-01-05 13:43:01 +00:00
const csv = require('csvtojson');
2018-12-06 11:52:54 +00:00
2019-01-05 13:43:01 +00:00
let csvdata;
2018-12-20 19:45:48 +00:00
2018-12-06 11:52:54 +00:00
csv()
.fromFile("world_data.csv")
.then((jsonObj) => {
2019-01-05 13:43:01 +00:00
csvdata = jsonObj;
});
2018-12-06 11:52:54 +00:00
2018-12-06 11:21:40 +00:00
/**************************************************************************
********************** handle HTTP METHODS ***********************
**************************************************************************/
2019-01-05 13:43:01 +00:00
2018-12-20 19:45:48 +00:00
// ------------------------------ GET ------------------------------
app.get("/items", function (req, res) {
2019-01-05 13:43:01 +00:00
res.send(csvdata);
2018-12-20 19:45:48 +00:00
});
2019-01-05 13:43:01 +00:00
2018-12-20 19:45:48 +00:00
app.get("/items/:id", function (req, res) {
2019-01-05 13:43:01 +00:00
const requested_id = parseInt(req.params.id);
let answer = "Err, No such id " + requested_id + " in database";
2018-12-20 19:45:48 +00:00
2019-01-05 13:43:01 +00:00
for (let i = 0; i < csvdata.length; i++) {
if (parseInt(csvdata[i].id) === requested_id) {
answer = csvdata[i];
2018-12-20 19:45:48 +00:00
}
}
res.send(answer);
});
2019-01-05 13:43:01 +00:00
app.get("/items/:id1/:id2", function (req, res) {
const first_requested_id = parseInt(req.params.id1);
const second_requested_id = parseInt(req.params.id2);
let answer = "Err, Range not possible";
if (id_exists(first_requested_id) && id_exists(second_requested_id)
&& first_requested_id < second_requested_id) {
answer = [];
for (let i = 0; i < csvdata.length; i++) {
if (parseInt(csvdata[i].id) >= first_requested_id) {
if (parseInt(csvdata[i].id) <= second_requested_id) {
answer.push(csvdata[i]);
}
2018-12-20 19:45:48 +00:00
}
}
}
res.send(answer);
});
2019-01-05 13:43:01 +00:00
app.get("/properties", function (req, res) {
res.send(Object.keys(csvdata[0]));
2018-12-20 19:45:48 +00:00
});
app.get("/properties/:num", function (req, res) {
let num = parseInt(req.params.num);
2019-01-05 13:43:01 +00:00
let answer = "Err, No such property available";
2018-12-20 19:45:48 +00:00
2019-01-05 13:43:01 +00:00
keys = Object.keys(csvdata[0]);
if (num >= 0 && num < keys.length) {
answer = keys[num];
2018-12-20 19:45:48 +00:00
}
res.send(answer);
2019-01-05 13:43:01 +00:00
});
2018-12-20 19:45:48 +00:00
// ----------------------------- POST ------------------------------
app.post("/items", function (req, res) {
2019-01-05 13:43:01 +00:00
const new_entry = req.body;
2018-12-21 08:25:32 +00:00
// set id
2019-01-05 13:43:01 +00:00
const last_id = parseInt(csvdata[csvdata.length - 1].id);
2018-12-21 08:25:32 +00:00
new_entry["id"] = (last_id + 1).toString();
2019-01-05 13:43:01 +00:00
// add entry to csvdata
csvdata.push(new_entry);
2018-12-21 08:25:32 +00:00
res.send("Added country " + new_entry.name + " to list!");
2018-12-20 19:45:48 +00:00
});
2019-01-05 13:43:01 +00:00
2018-12-20 19:45:48 +00:00
// ---------------------------- DELETE -----------------------------
app.delete("/items", function (req, res) {
2019-01-05 13:43:01 +00:00
const name = csvdata[csvdata.length - 1].name;
2018-12-21 08:25:32 +00:00
2019-01-05 13:43:01 +00:00
csvdata.splice(-1, 1);
2018-12-21 08:25:32 +00:00
res.send("Deleted last country " + name + "!");
2018-12-20 19:45:48 +00:00
});
2019-01-05 13:43:01 +00:00
app.delete("/items/:id", function (req, res) {
const requested_id = parseInt(req.params.id);
let answer = "Err, No such id " + requested_id + "in database";
2018-12-21 08:25:32 +00:00
2019-01-05 13:43:01 +00:00
for (let i = 0; i < csvdata.length; i++) {
if (parseInt(csvdata[i].id) === requested_id) {
csvdata.splice(i, 1);
answer = "Item " + requested_id + " deleted successfully.";
2018-12-21 08:25:32 +00:00
break;
}
}
2018-12-20 19:45:48 +00:00
res.send(answer);
});
2018-12-06 11:21:40 +00:00
// DO NOT CHANGE!
// bind server to port
2019-01-05 13:43:01 +00:00
const server = app.listen(3000, function () {
console.log('Example app listening at http://localhost:3000');
});
const id_exists = (id) => {
for (let i = 0; i < csvdata.length; i++) {
if (parseInt(csvdata[i].id) === first_requested_id) {
return true;
}
}
return false;
};