Aufgabe 3: Rest-API funktioniert
This commit is contained in:
parent
712688969e
commit
489f2c3baf
2 changed files with 71 additions and 77 deletions
143
aufgabe3/app.js
143
aufgabe3/app.js
|
@ -1,11 +1,9 @@
|
||||||
// DO NOT CHANGE!
|
// DO NOT CHANGE!
|
||||||
//init app with express, util, body-parser, csv2json
|
//init app with express, util, body-parser, csv2json
|
||||||
var express = require('express');
|
const express = require('express');
|
||||||
var app = express();
|
const app = express()
|
||||||
var sys = require('util');
|
const path = require('path');
|
||||||
var path = require('path');
|
const bodyParser = require('body-parser');
|
||||||
var bodyParser = require('body-parser');
|
|
||||||
var Converter = require("csvtojson").Converter;
|
|
||||||
|
|
||||||
//register body-parser to handle json from res / req
|
//register body-parser to handle json from res / req
|
||||||
app.use(bodyParser.json());
|
app.use(bodyParser.json());
|
||||||
|
@ -20,63 +18,55 @@ app.use(express.static(path.join(__dirname, "public")));
|
||||||
****************************** csv2json *********************************
|
****************************** csv2json *********************************
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
const csv = require('csvtojson')
|
const csv = require('csvtojson');
|
||||||
|
|
||||||
var json;
|
let csvdata;
|
||||||
|
|
||||||
csv()
|
csv()
|
||||||
.fromFile("world_data.csv")
|
.fromFile("world_data.csv")
|
||||||
.then((jsonObj) => {
|
.then((jsonObj) => {
|
||||||
json = jsonObj;
|
csvdata = jsonObj;
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
/**************************************************************************
|
/**************************************************************************
|
||||||
********************** handle HTTP METHODS ***********************
|
********************** handle HTTP METHODS ***********************
|
||||||
**************************************************************************/
|
**************************************************************************/
|
||||||
|
|
||||||
|
|
||||||
// ------------------------------ GET ------------------------------
|
// ------------------------------ GET ------------------------------
|
||||||
app.get("/items", function (req, res) {
|
app.get("/items", function (req, res) {
|
||||||
console.log(json);
|
res.send(csvdata);
|
||||||
res.send(json);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("/items/:id", function (req, res) {
|
app.get("/items/:id", function (req, res) {
|
||||||
var requested_id = parseInt(req.params.id);
|
const requested_id = parseInt(req.params.id);
|
||||||
var answer = ["Err", "No such id " + requested_id + " in database"];
|
let answer = "Err, No such id " + requested_id + " in database";
|
||||||
|
|
||||||
for (var i = 0; i < json.length; i++) {
|
for (let i = 0; i < csvdata.length; i++) {
|
||||||
if (json[i].id == requested_id) {
|
if (parseInt(csvdata[i].id) === requested_id) {
|
||||||
answer = [json[i]];
|
answer = csvdata[i];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(answer);
|
res.send(answer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
app.get("/items/:id1/:id2", function (req, res) {
|
app.get("/items/:id1/:id2", function (req, res) {
|
||||||
var first_requested_id = parseInt(req.params.id1);
|
const first_requested_id = parseInt(req.params.id1);
|
||||||
var second_requested_id = parseInt(req.params.id2);
|
const second_requested_id = parseInt(req.params.id2);
|
||||||
var answer = ["Err", "Range not possible"];
|
let answer = "Err, Range not possible";
|
||||||
|
|
||||||
// check that first id is bigger than 0
|
if (id_exists(first_requested_id) && id_exists(second_requested_id)
|
||||||
// and the second one is the bigger one
|
&& first_requested_id < second_requested_id) {
|
||||||
if (first_requested_id >= 0 && second_requested_id > first_requested_id) {
|
answer = [];
|
||||||
var enable_push = false;
|
for (let i = 0; i < csvdata.length; i++) {
|
||||||
|
if (parseInt(csvdata[i].id) >= first_requested_id) {
|
||||||
for (var i; i < json.length; i++) {
|
if (parseInt(csvdata[i].id) <= second_requested_id) {
|
||||||
// enable push when the first id is found
|
answer.push(csvdata[i]);
|
||||||
if (parseInt(json[i].id) == first_requested_id) {
|
}
|
||||||
enable_push = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// push key when enabled
|
|
||||||
if (enable_push) {
|
|
||||||
answer.push(json[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// break when second id is found
|
|
||||||
if (parseInt(json[i].id) == second_requested_id) {
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -84,66 +74,59 @@ app.get("/items/:id1/:id2", function (req, res) {
|
||||||
res.send(answer);
|
res.send(answer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.get("/properties", function (req, res) {
|
app.get("/properties", function (req, res) {
|
||||||
var answer = [];
|
res.send(Object.keys(csvdata[0]));
|
||||||
|
|
||||||
for (var key in json[0]) {
|
|
||||||
answer.push(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.send(answer);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/properties/:num", function (req, res) {
|
app.get("/properties/:num", function (req, res) {
|
||||||
let num = parseInt(req.params.num);
|
let num = parseInt(req.params.num);
|
||||||
var answer = ["Err", "No such property available"];
|
let answer = "Err, No such property available";
|
||||||
|
|
||||||
var i = 0;
|
keys = Object.keys(csvdata[0]);
|
||||||
|
if (num >= 0 && num < keys.length) {
|
||||||
for (var key in json[0]) {
|
answer = keys[num];
|
||||||
if (i == num) {
|
|
||||||
answer = [key];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send(answer);
|
res.send(answer);
|
||||||
})
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------- POST ------------------------------
|
// ----------------------------- POST ------------------------------
|
||||||
app.post("/items", function (req, res) {
|
app.post("/items", function (req, res) {
|
||||||
var new_entry = req.body;
|
const new_entry = req.body;
|
||||||
|
|
||||||
// set id
|
// set id
|
||||||
var last_id = parseInt(json[json.length - 1].id);
|
const last_id = parseInt(csvdata[csvdata.length - 1].id);
|
||||||
new_entry["id"] = (last_id + 1).toString();
|
new_entry["id"] = (last_id + 1).toString();
|
||||||
|
|
||||||
// add entry to json
|
// add entry to csvdata
|
||||||
json.push(new_entry);
|
csvdata.push(new_entry);
|
||||||
|
|
||||||
res.send("Added country " + new_entry.name + " to list!");
|
res.send("Added country " + new_entry.name + " to list!");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------- DELETE -----------------------------
|
// ---------------------------- DELETE -----------------------------
|
||||||
app.delete("/items", function (req, res) {
|
app.delete("/items", function (req, res) {
|
||||||
var name = json[json.length - 1].name;
|
const name = csvdata[csvdata.length - 1].name;
|
||||||
|
|
||||||
json.splice(-1, 1);
|
csvdata.splice(-1, 1);
|
||||||
|
|
||||||
res.send("Deleted last country " + name + "!");
|
res.send("Deleted last country " + name + "!");
|
||||||
});
|
});
|
||||||
|
|
||||||
app.delete("/items/id", function (req, res) {
|
app.delete("/items/:id", function (req, res) {
|
||||||
var requested_id = parseInt(req.params.id);
|
const requested_id = parseInt(req.params.id);
|
||||||
var answer = ["Err", "No such id " + requested_id + "in database"];
|
let answer = "Err, No such id " + requested_id + "in database";
|
||||||
|
|
||||||
for (var i = 0; i < json.length; i++) {
|
|
||||||
if (json[i].id == requested_id) {
|
|
||||||
json.splice(i, 1);
|
|
||||||
answer = ["Item " + requested_id + " deleted successfully."];
|
|
||||||
|
|
||||||
|
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.";
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,8 +136,18 @@ app.delete("/items/id", function (req, res) {
|
||||||
|
|
||||||
// DO NOT CHANGE!
|
// DO NOT CHANGE!
|
||||||
// bind server to port
|
// bind server to port
|
||||||
var server = app.listen(3000, function () {
|
const server = app.listen(3000, function () {
|
||||||
var host = server.address().address;
|
console.log('Example app listening at http://localhost:3000');
|
||||||
var port = server.address().port;
|
|
||||||
console.log('Example app listening at http://%s:%s', host, port);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const id_exists = (id) => {
|
||||||
|
for (let i = 0; i < csvdata.length; i++) {
|
||||||
|
if (parseInt(csvdata[i].id) === first_requested_id) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
|
@ -4,7 +4,8 @@
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "app.js",
|
"main": "app.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
|
"start": "node app.js"
|
||||||
},
|
},
|
||||||
"author": "TEAM_00",
|
"author": "TEAM_00",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
|
|
Loading…
Reference in a new issue