From 6aec705ff723dea564104290e2ab3b15631c756b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-VKHHMCQ=5CHenri=20Fran=C3=A7ois?= Date: Sun, 16 Dec 2018 21:01:19 +0100 Subject: [PATCH 1/2] valider --- answers.md => Henri Francois.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename answers.md => Henri Francois.md (100%) diff --git a/answers.md b/Henri Francois.md similarity index 100% rename from answers.md rename to Henri Francois.md From 0c6fa4bfdee6aa24d44b53681deb16a851951e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?DESKTOP-VKHHMCQ=5CHenri=20Fran=C3=A7ois?= Date: Sun, 16 Dec 2018 23:18:31 +0100 Subject: [PATCH 2/2] Final WORK --- Henri Francois.md | 51 ++- dockerfile | 7 + index.js | 1069 +++++++++++++++++++++++++++++++++++++++++++++ indexfinal.js | 1000 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 2125 insertions(+), 2 deletions(-) create mode 100644 dockerfile create mode 100644 index.js create mode 100644 indexfinal.js diff --git a/Henri Francois.md b/Henri Francois.md index b7025b97..63730b61 100644 --- a/Henri Francois.md +++ b/Henri Francois.md @@ -1,41 +1,88 @@ # Answers -Lastname: -Firstname: +Lastname: FRANCOIS +Firstname: HENRI ## 2.2 command: +docker build . -t hey +docker run hey + ## 2.3 question: + +It is not possible through postman to access the container, indeed the port isn't opened. + command: +docker run -p 3000:3000 -td hey + ## 2.5 question: + +In order to push an image to our dockhub repo, we have to login and then tag the image. + command: +docker login +docker tag indexfinal.js Henri000/devops_lab + ## 2.6 command: +docker system prune -a + question: + +Before container start, we pull the image. + command: +docker create Henri000/devops_lab + command: +docker run --detach Henri000/devops_lab + ## 2.7 question: + +We can see the container's names, ids, when they were created and their status. question: command: +docker ps + command: +docker rename jovial_yalow application +docker run --detach Henri000/devops-lab:test1 + ## 2.8 question: + +we want to to find the info about the current OS running our DOcker container + output: +NAME="Alpine Linux" +ID=alpine +VERSION_ID=3.8.1 +PRETTY_NAME="Alpine Linux v3.8" +HOME_URL="http://alpinelinux.org" +BUG_REPORT_URL="http://bugs.alpinelinux.org" + ## 3.1 command: +docker-compose up + ## 3.4 command: + +docker-compose up -d + command: + +docker-compose logs \ No newline at end of file diff --git a/dockerfile b/dockerfile new file mode 100644 index 00000000..f9c1dd40 --- /dev/null +++ b/dockerfile @@ -0,0 +1,7 @@ +FROM node +WORKDIR /app +COPY indexfinal.js /app +RUN npm install mysql +RUN npm install express +EXPOSE 3000 +CMD node indexfinal.js \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 00000000..4a0b6ce7 --- /dev/null +++ b/index.js @@ -0,0 +1,1069 @@ +const express = require('express'); +const mysql = require('mysql'); +const bodyParser = require('body-parser'); +const app = express(); + +app.use(bodyParser.urlencoded({ extended: true })); + +var db = mysql.createConnection({ +host: process.env.MYSQL_HOST, +user: process.env.MYSQL_LOGIN, +password: process.env.MYSQL_PASSWORD, +database: process.env.MYSQL_DATABASE, +port: process.env.MYSQL_PORT +}); + +//FIREWALL +app.use(function(req, res, next) { + if ("key" in req.query) { + var key = req.query["key"]; + var query = "SELECT * FROM users WHERE apikey='" + key + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err; + if (result.length > 0) { + next(); + } + else { + res.send("ERROR",403); + } + }); + } else { + res.send("ERROR",403); + } +}); + + + +//animal +app.post('/animals', function(req, res) { + var name = req.body.name; + var breed = req.body.breed; + var food = req.body.food_per_day; + var birthday = req.body.birthday; + var entry = req.body.entry_date; + var id_cage = req.body.id_cage; + + var query = "INSERT INTO animals (name,breed,food_per_day,birthday,entry_date,id_cage) VALUES ('"+name+"','"+breed+"',"+food+",'"+birthday+"','"+entry+"',"+id_cage+")"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("success")); + }); +}); + +app.get('/animals', function(req, res) { + + var query = "SELECT * FROM animals"; + + //SELECTION + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id', function(req, res) { + + var id = req.params.id; + var query = "SELECT * FROM animals WHERE id="+ id; + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.put('/animals/:id', function(req, res) { + + var id = req.params.id; + var name = req.body.name; + var breed = req.body.breed; + var food = req.body.food_per_day; + var birthday = req.body.birthday; + var entry = req.body.entry_date; + var id_cage = req.body.id_cage; + + var query = "UPDATE animals SET "; + + if(name != undefined) query += " name = '"+name+"',"; + if(breed != undefined) query += " breed = '"+breed+"',"; + if(food != undefined) query += " food_per_day = '"+food+"',"; + if(birthday != undefined) query += " birthday = '"+birthday+"',"; + if(entry != undefined) query += " entry_date = '"+entry+"',"; + if(id_cage != undefined) query += " id_cage = '"+id_cage+"',"; + + query = query.slice(0,-1); + + query += " WHERE id = "+id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/animals', function(req, res) { + var query = "DELETE FROM animals" + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/animals/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM animals WHERE id = " + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + + + +//cages +app.post('/cages', function(req, res) { + var name = req.body.name; + var description = req.body.description; + var area = req.body.area; + + var query = "INSERT INTO cages (name,description,area) VALUES ('"+name+"','"+description+"',"+area+")"; + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify("success")); + }); +}); + +app.get('/cages', function(req, res) { + + var query = "SELECT * FROM cages"; + + //SELECTION + var conditions = ["name", "description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id', function(req, res) { + + var id = req.params.id; + var query = "SELECT * FROM cages WHERE id=" + id; + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.put('/cages/:id', function(req, res) { + var id = req.params.id; + var name = req.body.name; + var description = req.body.description; + var area = req.body.area; + + var query = "UPDATE cages SET "; + + if(name != undefined) query += " name = '"+name+"',"; + if(description != undefined) query += " description = '"+description+"',"; + if(area != undefined) query += " area = '"+area+"',"; + + query = query.slice(0,-1); + + query += " WHERE id = "+id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/cages', function(req, res) { + var query = "DELETE FROM cages" + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/cages/:id', function(req, res) { + + var id = req.params.id; + var query = "DELETE FROM cages WHERE id = " + id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + + + + +//food +app.post('/food', function(req, res) { + var name = req.body.name; + var quantity = req.body.quantity; + var id_animal = req.body.id_animal; + + var query = "INSERT INTO food (name,quantity,id_animal) VALUES ('"+name+"',"+quantity+","+id_animal+")"; + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify("success")); + }); +}); + +app.get('/food', function(req, res) { + + + var query = "SELECT * FROM food"; + + //SELECTION + var conditions = ["name", "quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id', function(req, res) { + + var id = req.params.id; + + var query = "SELECT * FROM food WHERE id=" + id; + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.put('/food/:id', function(req, res) { + var id = req.params.id; + var name = req.body.name; + var quantity = req.body.quantity; + var id_animal = req.body.id_animal; + + var query = "UPDATE food SET "; + + if(name != undefined) query += " name = '"+name+"',"; + if(quantity != undefined) query += " quantity = '"+quantity+"',"; + if(id_animal != undefined) query += " id_animal = '"+id_animal+"',"; + + query = query.slice(0,-1); + + query += " WHERE id = "+id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/food', function(req, res) { + var query = "DELETE FROM food" + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/food/:id', function(req, res) { + + var id = req.params.id; + var query = "DELETE FROM food WHERE id = " + id; + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + + +//staff +app.post('/staff', function(req, res) { + var firstname = req.body.firstname; + var lastname = req.body.lastname; + var wage = req.body.wage; + + var query = "INSERT INTO staff (firstname,lastname,wage) VALUES ('"+firstname+"','"+lastname+"',"+wage+")"; + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify("success")); + }); +}); + +app.get('/staff', function(req, res) { + + var query = "SELECT * FROM staff"; + + //SELECTION + var conditions = ["firstname", "lastname","wage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/staff/:id', function(req, res) { + + var id = req.params.id; + var query = "SELECT * FROM staff WHERE id=" + id; + + //SORTING + if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + + for (var index in sort) { + + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + + query += " " + field; + + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + + query = query.slice(0, -1); + + } + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.put('/staff/:id', function(req, res) { + var id = req.params.id; + var firstname = req.body.firstname; + var lastname = req.body.lastname; + var wage = req.body.wage; + + var query = "UPDATE staff SET "; + + if(firstname != undefined) query += " firstname = '"+firstname+"',"; + if(lastname != undefined) query += " lastname = '"+lastname+"',"; + if(wage != undefined) query += " wage = '"+wage+"',"; + + query = query.slice(0,-1); + + query += " WHERE id = "+id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/staff', function(req, res) { + var query = "DELETE FROM staff" + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.delete('/staff/:id', function(req, res) { + + var id = req.params.id; + var query = "DELETE FROM staff WHERE id = " + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +//read food-stat + +app.get('/food-stats', function(req, res) { + + var query ="SELECT animals.id, CASE WHEN animals.food_per_day = 0 THEN 0 ELSE food.quantity/animals.food_per_day END as days_left FROM food INNER JOIN animals ON food.id_animal = animals.id"; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + + +//relationships + +//cages in animal + +app.get('/animals/:id/cages', function(req, res) { + var id = req.params.id; + + var query = "SELECT cages.* FROM animals INNER JOIN cages ON animals.id_cage = cages.id WHERE animals.id =" + id; + + //SELECTION + var conditions = ["id","name", "description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " cages." + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id_animals/cages/:id_cage', function(req, res) { + + var id_animals = req.params.id_animals; + var id_cage = req.params.id_cage; + + var query = "SELECT cages.* FROM animals INNER JOIN cages ON animals.id_cage = cages.id WHERE animals.id=" + id_animals + " AND cages.id=" + id_cage; + + //SELECTION + var conditions = ["id","name", "description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " cages." + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +//animal in cages + +app.get('/cages/:id/animals', function(req, res) { + var id = req.params.id; + + var query = "SELECT animals.* FROM animals INNER JOIN cages ON animals.id_cage = cages.id WHERE cages.id =" + id; + + //SELECTION + var conditions = ["id","name", "breed","food_per_day","birthday","entry_date","id_cage"]; + + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " animals." + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + +//food in animal + +app.get('/animals/:id/food', function(req, res) { + var id = req.params.id; + + var query = "SELECT food.* FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id=" + id; + + //SELECTION + var conditions = ["id","name", "quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +//animal in food + +app.get('/food/:id/animals', function(req, res) { + var id = req.params.id; + + var query = "SELECT animals.* FROM food INNER JOIN animals ON food.id_animal = animals.id WHERE food.id=" + id; + + //SELECTION + var conditions = ["id","name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id_food/animals/:id_animals', function(req, res) { + + var id_animals = req.params.id_animals; + var id_food = req.params.id_food; + + var query = "SELECT animals.* FROM food INNER JOIN animals ON food.id_animal = animals.id WHERE food.id=" + id_food + " AND animals.id=" + id_animals; + + //SELECTION + var conditions = ["id","name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + + //FILTERS + if ("fields" in req.query) { + + query = query.replace("*", req.query["fields"]); + } + + //PAGINATION + if ("limit" in req.query) { + + query += " LIMIT " + req.query["limit"]; + + if ("offset" in req.query) { + + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result, fields) { + + if (err) throw err; + + res.send(JSON.stringify(result)); + }); +}); + + + +app.listen(3000, function() { + db.connect(function(err) { + if (err) throw err; + console.log('Connection to database successful!'); + }); + console.log('Example app listening on port 3000!'); +}); + diff --git a/indexfinal.js b/indexfinal.js new file mode 100644 index 00000000..5a985de9 --- /dev/null +++ b/indexfinal.js @@ -0,0 +1,1000 @@ +const express = require('express'); +const mysql = require('mysql'); +const bodyParser = require('body-parser'); +const app = express(); + +app.use(bodyParser.urlencoded({extended : true})); + +var db = mysql.createConnection({ + host: "localhost", + user: "root", + password: "", + database: "zoo2", + port: "3306" +}); + +//FIREWALL +app.use(function(req, res, next) { + if ("key" in req.query) { + var key = req.query["key"]; + var query = "SELECT * FROM users WHERE apikey='" + key + "'"; + db.query(query, function(err, result, fields) { + if (err) throw err; + if (result.length > 0) { + next(); + } + else { + res.status(403).send("Access denied"); + } + }); + } else { + res.status(403).send("Access denied"); + } +}); + +//---------------------------------------------------########## +app.post('/staff',function (req,res){ + let firstname = req.body.firstname; + let lastname = req.body.lastname; + let wage = req.body.wage; + let query = "insert into STAFF(firstname,lastname,wage)" + + " values('"+firstname+"','"+lastname+"',"+wage+")"; + db.query(query,function (err) { + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.get('/staff',function(req,res){ + var query = "SELECT * FROM staff"; + var conditions = ["id", "firstname","lastname","wage"]; + for (let index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (let index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function(err, result) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/staff/:id',function(req,res){ + let id = req.params.id; + let query = "SELECT * FROM staff WHERE id="+id; + var conditions = ["id", "firstname","lastname","wage"]; + for (let index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (let index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +app.put('/staff/:id',function (req,res) { + let id = req.params.id; + let firstname = req.body.firstname; + let lastname = req.body.lastname; + let wage = req.body.wage; + let query = "update staff set "; + if(firstname !== undefined) query+="firstname = '"+firstname+"',"; + if(lastname !== undefined) query+="lastname = '"+lastname+"',"; + if(wage !== undefined) query+="wage = "+wage+","; + query = query.slice(0,-1); + query += " where id="+id; + db.query(query,function(err,result){ + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/staff', function(req, res) { + var query = "DELETE FROM staff"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/staff/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM staff WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + + + +////////////////////////////////////////////////////////////// +app.post('/animals',function (req,res){ + let name = req.body.name; + let breed = req.body.breed; + let food_per_day = req.body.food_per_day; + let birthday = req.body.birthday; + let entry_date = req.body.entry_date; + let id_cage = req.body.id_cage; + let query = "insert into animals(name,breed,food_per_day,birthday,entry_date,id_cage)" + + " values('"+name+"','"+breed+"',"+food_per_day+",'"+birthday+"','"+entry_date+"',"+id_cage+")"; + db.query(query,function (err) { + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.get('/animals',function(req,res){ + var query = "SELECT * FROM animals"; + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id',function(req,res){ + let id = req.params.id; + let query = "SELECT * FROM animals WHERE id="+id; + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id/cages',function(req,res){ + let id = req.params.id; + let query = + "SELECT cages.id, cages.name, cages.description, cages.area " + + "FROM animals INNER JOIN cages ON cages.id = animals.id_cage " + + "WHERE animals.id="+id; + var conditions = ["id", "name","description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("cages.id, cages.name, cages.description, cages.area", "cages."+req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id/cages/:id_cage',function(req,res){ + let id = req.params.id; + let id_cage = req.params.id_cage; + let query = + "SELECT cages.id, cages.name, cages.description, cages.area " + + "FROM animals INNER JOIN cages ON cages.id = animals.id_cage " + + "WHERE animals.id="+id+" AND cages.id="+id_cage; + var conditions = ["id", "name","description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("cages.id, cages.name, cages.description, cages.area", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.put('/animals/:id',function (req,res) { + let id = req.params.id; + let name = req.body.name; + let breed = req.body.breed; + let food_per_day = req.body.food_per_day; + let birthday = req.body.birthday; + let entry_date = req.body.entry_date; + let id_cage = req.body.id_cage; + let query = "update animals set "; + if(name !== undefined) query+="name = '"+name+"',"; + if(breed !== undefined) query+="breed = '"+breed+"',"; + if(food_per_day !== undefined) query+="food_per_day = "+food_per_day+","; + if(birthday !== undefined) query+="birthday = '"+birthday+"',"; + if(entry_date !== undefined) query+="entry_date = '"+entry_date+"',"; + if(id_cage !== undefined) query+="id_cage = "+id_cage+","; + query = query.slice(0,-1); + query += " where id="+id; + db.query(query,function(err,result){ + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.get('/animals/:id/food',function(req,res){ + let id = req.params.id; + let query = + "SELECT food.id, food.name, food.quantity, food.id_animal " + + "FROM animals INNER JOIN food ON food.id_animal = animals.id " + + "WHERE animals.id="+id; + var conditions = ["id", "name","quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("food.id, food.name, food.quantity, food.id_animal", "food."+req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/animals/:id/food/:id_food',function(req,res){ + let id = req.params.id; + let id_food = req.params.id_food; + let query = + "SELECT food.id, food.name, food.quantity, food.id_animal " + + "FROM animals INNER JOIN food ON food.id_animal = animals.id " + + "WHERE animals.id="+id+" AND food.id="+id_food; + var conditions = ["id", "name","quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("food.id, food.name, food.quantity, food.id_animal", "food."+req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +app.delete('/animals', function(req, res) { + var query = "DELETE FROM animals"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/animals/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM animals WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +//////////////////////////////////////////////// + +app.post('/cages',function (req,res){ + let name = req.body.name; + let description = req.body.description; + let area = req.body.area; + let query = "insert into cages(name,description,area)" + + " values('"+name+"','"+description+"',"+area+")"; + db.query(query,function (err) { + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + + +app.get('/cages',function(req,res){ + var query = "SELECT * FROM cages"; + var conditions = ["id", "name","description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id',function(req,res){ + let id = req.params.id; + let query = "SELECT * FROM cages WHERE id="+id; + var conditions = ["id", "name","description","area"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id/animals',function(req,res){ + let id = req.params.id; + let query = + "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage "+ + "FROM cages INNER JOIN animals ON cages.id = animals.id_cage " + + "WHERE cages.id="+id; + var conditions = ["id","name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage", "animals."+req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + // console.log(query); + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/cages/:id/animals/:id_animals',function(req,res){ + let id = req.params.id; + let id_animals = req.params.id_animals; + let query = + "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage "+ + "FROM animals INNER JOIN cages ON cages.id = animals.id_cage " + + "WHERE cages.id="+id+" AND animals.id="+id_animals; + var conditions = ["id","name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage", "animals."+req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + // console.log(query); + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +app.put('/cages/:id',function (req,res) { + let id = req.params.id; + let name = req.body.name; + let description = req.body.description; + let area = req.body.area; + let query = "update cages set "; + if(name !== undefined) query+="name = '"+name+"',"; + if(description !== undefined) query+="description = '"+description+"',"; + if(area !== undefined) query+="area = "+area+","; + query = query.slice(0,-1); + query += " where id="+id; + db.query(query,function(err,result){ + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/cages', function(req, res) { + var query = "DELETE FROM cages"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/cages/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM cages WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +/////////////////////////////////////////////////////////////// + +app.post('/food',function (req,res){ + let name = req.body.name; + let quantity = req.body.quantity; + let id_animal = req.body.id_animal; + let query = "insert into food(name,quantity,id_animal)" + + " values('"+name+"',"+quantity+","+id_animal+")"; + // res.send(req.body); + // res.send(query); + db.query(query,function (err) { + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.get('/food',function(req,res){ + var query = "SELECT * FROM food"; + var conditions = ["id", "name","quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + + db.query(query, function(err, result) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id',function(req,res){ + let id = req.params.id; + let query = "SELECT * FROM food WHERE id="+id; + var conditions = ["id", "name","quantity","id_animal"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id/animals',function(req,res){ + let id = req.params.id; + let query = + "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, " + + "animals.birthday, animals.entry_date, animals.id_cage " + + "FROM food INNER JOIN animals ON food.id_animal = animals.id " + + "WHERE food.id="+id; + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage", "animals."+req.query["fields"]); + // query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +app.get('/food/:id/animals/:id_animal',function(req,res){ + let id = req.params.id; + let id_animal = req.params.id_animal; + let query = + // "SELECT animals.*"+ + // "FROM food INNER JOIN animals ON food.id_animal = animals.id " + + // "WHERE food.id="+id+" AND animals.id="+id_animal; + "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, " + + "animals.birthday, animals.entry_date, animals.id_cage " + + "FROM food INNER JOIN animals ON food.id_animal = animals.id " + + "WHERE food.id="+id+" AND animals.id="+id_animal; + + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; + for (var index in conditions) { + if (conditions[index] in req.query) { + if (query.indexOf("WHERE") < 0) { + query += " WHERE"; + } else { + query += " AND"; + } + query += " " + conditions[index] + "='" + + req.query[conditions[index]] + "'"; + } + } + if ("sort" in req.query) { + var sort = req.query["sort"].split(","); + query += " ORDER BY"; + for (var index in sort) { + var direction = sort[index].substr(0, 1); + var field = sort[index].substr(1); + query += " " + field; + if (direction == "-") + query += " DESC,"; + else + query += " ASC,"; + } + query = query.slice(0, -1); + } + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date, animals.id_cage", "animals."+req.query["fields"]); + // query = query.replace("*", req.query["fields"]); + } + if ("limit" in req.query) { + query += " LIMIT " + req.query["limit"]; + if ("offset" in req.query) { + query += " OFFSET " + req.query["offset"]; + } + } + // console.log(query); + db.query(query, function (err,result){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +app.put('/food/:id',function (req,res) { + let id = req.params.id; + let name = req.body.name; + let quantity = req.body.quantity; + let id_animal = req.body.id_animal; + let query = "update food set "; + if(name !== undefined) query+="name = '"+name+"',"; + if(quantity !== undefined) query+="quantity = '"+quantity+"',"; + if(id_animal !== undefined) query+="id_animal = "+id_animal+","; + query = query.slice(0,-1); + query += " where id="+id; + db.query(query,function(err,result){ + if(err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +app.delete('/food', function(req, res) { + var query = "DELETE FROM food"; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +app.delete('/food/:id', function(req, res) { + var id = req.params.id; + var query = "DELETE FROM food WHERE id=" + id; + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); +///////////////////////////////////////////////////////////////////////////////////////////////////////////// +app.get('/food-stats', function(req, res) { + + db.query("SELECT animals.id," + + "(case when animals.food_per_day = 0 then 0 " + + "else (food.quantity/animals.food_per_day)end ) as days_left " + + "FROM food INNER JOIN animals ON food.id_animal=animals.id", function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); +}); + +//////////////////////////////////////////////////////////////// +app.listen(3000,function (){ + db.connect(function (err) { + if(err) throw err; + console.log('Connection to database successful'); + }); + console.log("Example app listening on port 3000"); +}); + +