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