From a531a5a18e4678129595845795c3e208b2a19e40 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:53:45 +0100 Subject: [PATCH 1/8] Add files via upload Added project.js --- project.js | 1379 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1379 insertions(+) create mode 100644 project.js diff --git a/project.js b/project.js new file mode 100644 index 00000000..8005e436 --- /dev/null +++ b/project.js @@ -0,0 +1,1379 @@ +// TP1 WEB + +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: "root", +database: "project", +port: "8889" +}); + +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("You do not have rights to visit this page") + } + }); +} +else { + res.status(403).send("You do not have rights to visit this page") +} +}); + + + +//------------------------------------------------------------------------------------------------------------------------------------------------// +// // +// TP zoo // +// // +//------------------------------------------------------------------------------------------------------------------------------------------------// + +// GET / POST / PUT / DELETE FOR ANIMALS + +//SELECT ALL ANIMALS + +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; + +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"]; +} +} + + +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + +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; + +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"]; +} +} + + +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + +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; + + +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"]; +} +} +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + +app.get('/cages/:id/animals', function(req, res) { + + var id = req.params.id; + +var query="SELECT animals.* from cages INNER JOIN animals ON cages.id = animals.id_cage WHERE cages.id="+id; + + +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"]; +} +} + + +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + +app.get('/animals/:id1/cages/:id2', function(req, res) { + + var id1 = req.params.id1; +var id2 = req.params.id2; +var query="SELECT cages.* from animals INNER JOIN cages ON animals.id_cage = cages.id WHERE animals.id="+id1 +" AND cages.id="+id2; + +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"]; +} +} + + +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + + + +app.get('/food/:id1/animals/:id2', function(req, res) { + + var id1 = req.params.id1; +var id2 = req.params.id2; +var query="SELECT animals.* from food INNER JOIN animals ON food.id_animal = animals.id WHERE food.id="+id1 +" AND animals.id="+id2; + +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"]; +} +} + +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]] + "'"; +} +} + + console.log(query); + + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + + + + + + + + + + + + + + +app.get('/animals', function(req, res) { + +var query="SELECT * from animals"; + + + +// SELECT BY conditions + +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, fields) { + if (err) throw err; + + + res.send(JSON.stringify(result)); +}); }); + + +// SELECT ANIMAL WITH ID + +app.get('/animals/:id', function(req, res) { + + var id = req.params.id; + + var query= "SELECT * FROM animals "; + + + + +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"]; +} +} + + + query+=" WHERE id="+id; + + console.log(query); + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + +// ADD ANIMAL + +app.post('/animals',function(req,res){ + +var name = req.body.name; +var breed = req.body.breed; +var food_per_day = req.body.food_per_day; +var id_cage = req.body.id_cage; +var birthday = req.body.birthday; +var entry_date = req.body.entry_date; + + +var 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, result, fields) { + +if (err) throw err; +res.send(JSON.stringify("Success")); +}); }); + + +// UPDATE ANimal +app.put('/animals/:id', function(req, res) { + + +var id = req.params.id; +var name = req.body.name; +var breed = req.body.breed; +var food_per_day = req.body.food_per_day; +var birthday = req.body.birthday; +var entry_date= req.body.entry_date; +var id_cage = req.body.id_cage; + +var query = "UPDATE animals SET id="+id; + +if ("name" in req.body) +{ + +query+=", name = '"+name+"'"; + +} +if ("breed" in req.body) +{ + +query+=", breed = '"+breed+"'"; + +} +if ("food_per_day" in req.body) +{ + +query+=", food_per_day = '"+food_per_day+"'"; + +} +if ("birthday" in req.body) +{ + +query+=", birthday = '"+birthday+"'"; + +} +if ("entry_date" in req.body) +{ + +query+=", entry_date = '"+entry_date+"'"; + +} +if ("id_cage" in req.body) +{ + +query+=", id_cage = '"+id_cage+"'"; + +} +query+=" WHERE id="+id; + +console.log(query); + + +db.query(query, function(err,result,fields) { +if (err) throw err; +res.send(JSON.stringify("Success")); + +}); +}); + + +// DELETE ALL animal +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")); + +});}); + + +// DELETE animal BY ID +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")); +});}); + + +// GET / POST / PUT / DELETE FOR Cage ------------------------------------------------------------------------------------------------------------------------------- + + +//SELECT ALL Cage + +app.get('/cages', function(req, res) { + + +var query="SELECT * from cages"; + +// SELECT BY conditions + +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]]+"'"; + } +} +console.log(query); +//BY ORDER BY + +if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + console.log(query); + 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); +} + +console.log(query); + + +//FILTRE BY FIelds + +if ("fields" in req.query) { +query = query.replace("*", req.query["fields"]); +} + +console.log(query); + +//FILTRE BY Pagination + + +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, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); +}); }); + + +// SELECT CAGE WITH ID + +app.get('/cages/:id', function(req, res) { + + var id = req.params.id; + + var query= "SELECT * FROM cages "; + + + + +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"]; +} +} + + + query+=" WHERE id="+id; + + console.log(query); + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + + +app.get('/cages/:id/animals', function(req, res) { + + var id = req.params.id; + var query= "SELECT name,breed,id_cage FROM animals WHERE id_cage="+id; + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + res.send(JSON.stringify(result)); +}); }); + + + +// ADD cage + +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")); +}); }); + + +// UPDATE Cage +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 id="+ID; + +if ("name" in req.body) +{ + +query+=", name = '"+name+"'"; + +} +if ("description" in req.body) +{ + +query+=", description = '"+description+"'"; + +} +if ("area" in req.body) +{ + +query+=", area = '"+area+"'"; + +} + + +query+=" WHERE id="+ID; + +console.log(query); + + console.log(query); +db.query(query, function(err,result,fields) { +if (err) throw err; +res.send(JSON.stringify("Success")); + +}); +}); + +// DELETE ALL cage +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")); + +});}); + + +// DELETE cage BY ID +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")); +});}); + + +// GET / POST / PUT / DELETE FOR Foood ------------------------------------------------------------------------------------------------------------------------------- + + +//SELECT ALL Food +app.get('/food', function(req, res) { + + +var query="SELECT * from food"; + +// SELECT BY conditions + +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]]+"'"; + } +} +console.log(query); +//BY ORDER BY + +if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + console.log(query); + 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); +} + +console.log(query); + + +//FILTRE BY FIelds + +if ("fields" in req.query) { +query = query.replace("*", req.query["fields"]); +} + +console.log(query); + +//FILTRE BY Pagination + + +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, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); +}); }); + + +// SELECT FOOD WITH ID + +app.get('/food/:id', function(req, res) { + + var id = req.params.id; + + var query= "SELECT * FROM food "; + + + + +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"]; +} +} + + + query+=" WHERE id="+id; + + console.log(query); + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + + +// ADD food + +app.post('/food',function(req,res){ + +var name = req.body.name; +var id_animal= req.body.id_animal; +var quantity = req.body.quantity; + + + +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")); +}); }); + + +// UPDATE food +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 id="+ID; + +if ("name" in req.body) +{ + +query+=", name = '"+name+"'"; + +} +if ("quantity" in req.body) +{ + +query+=", quantity = '"+quantity+"'"; + +} +if ("id_animal" in req.body) +{ + +query+=",id_animal = '"+id_animal+"'"; + +} +query+=" WHERE id="+ID; + + + + console.log(query); +db.query(query, function(err,result,fields) { +if (err) throw err; +res.send(JSON.stringify("Success")); + +}); +}); + +// DELETE ALL food +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")); + +});}); + + +// DELETE food BY ID +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")); +});}); + + +// GET / POST / PUT / DELETE FOR STAFF ------------------------------------------------------------------------------------------------------------------------------- + + +//SELECT ALL STAFF + +app.get('/staff', function(req, res) { + +var query="SELECT * from staff"; + +// SELECT BY conditions + +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]]+"'"; + } +} +console.log(query); +//BY ORDER BY + +if ("sort" in req.query) { + + var sort = req.query["sort"].split(","); + + query += " ORDER BY"; + console.log(query); + 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); +} + +console.log(query); + + +//FILTRE BY FIelds + +if ("fields" in req.query) { +query = query.replace("*", req.query["fields"]); +} + +console.log(query); + +//FILTRE BY Pagination + + +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, fields) { + if (err) throw err; + + res.send(JSON.stringify(result)); +}); }); + + +// SELECT STAFF WITH ID + + +app.get('/staff/:id', function(req, res) { + + var id = req.params.id; + + var query= "SELECT * FROM staff "; + + + + +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"]; +} +} + + + query+=" WHERE id="+id; + + console.log(query); + + db.query(query, function(err, result, fields) { + if (err) throw err; + + + + + res.send(JSON.stringify(result)); +}); }); + + + +// ADD 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")); +}); }); + + +// UPDATE staff +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 id="+ID; + +if ("firstname" in req.body) +{ + +query+=", firstname = '"+firstname+"'"; + +} +if ("lastname" in req.body) +{ + +query+=", lastname = '"+lastname+"'"; + +} +if ("wage" in req.body) +{ + +query+=", wage = '"+wage+"'"; + +} +query+=" WHERE id="+ID; + +console.log(query); + + console.log(query); +db.query(query, function(err,result,fields) { +if (err) throw err; +res.send(JSON.stringify("Success")); + +}); +}); + + + + + +// DELETE ALL staff +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")); + +});}); + + +// DELETE staff BY ID +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")); +});}); + +// GET / POST / PUT / RELATION ANIMAL CAGES--------------------------------------------------------------------------------- + + + + + + + + + + + + + + + + + +// GET / POST / PUT / FOOD-STAT------------------------------------------------------------------------------------------------------------------------------- + + +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!'); +}); + + From 75d54ff52d01d92c8920b4e76598a70b4840db86 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 17:57:15 +0100 Subject: [PATCH 2/8] Update answers.md --- answers.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/answers.md b/answers.md index b7025b97..8752ffbc 100644 --- a/answers.md +++ b/answers.md @@ -1,26 +1,25 @@ # Answers -Lastname: -Firstname: +Lastname: Paban +Firstname: Louis ## 2.2 -command: +command: sudo docker run app ## 2.3 -question: -command: +question: Ports are not opened +command: sudo docker run -p 3000:3000 project.js ## 2.5 question: -command: +command: sudo docker tag project.js louispaban/devops_lab + sudo docker push louispaban/devops_lab ## 2.6 -command: - +command: docker system prune -a question: -command: - -command: +command: docker pull louispaban/devops-lab +command: docker create louispaban/devops-lab ## 2.7 question: @@ -32,10 +31,11 @@ command: ## 2.8 question: output: + ## 3.1 -command: +command: ## 3.4 -command: -command: +command: +command: From e3f94b6fff8f5a783423c6e0e2f02aa1ec03d65c Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 20:24:33 +0100 Subject: [PATCH 3/8] Update answers.md --- answers.md | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/answers.md b/answers.md index 8752ffbc..ed606c73 100644 --- a/answers.md +++ b/answers.md @@ -8,11 +8,11 @@ command: sudo docker run app ## 2.3 question: Ports are not opened -command: sudo docker run -p 3000:3000 project.js +command: sudo docker run -p 3000:3000 -td app ## 2.5 -question: -command: sudo docker tag project.js louispaban/devops_lab +question: change name in order to identify the repository +command: sudo docker tag 38650435o169 louispaban/devops_lab sudo docker push louispaban/devops_lab ## 2.6 @@ -20,22 +20,21 @@ command: docker system prune -a question: command: docker pull louispaban/devops-lab command: docker create louispaban/devops-lab + docker run louispaban/devops-lab ## 2.7 -question: -question: -command: +question: +question: The name of the container is serene_wescoff. +command: sudo docker ps -a -command: +command: sudo docker rename serene_wescoff cont_app ## 2.8 -question: -output: - + ## 3.1 -command: +command: command: sudo docker-compose up ## 3.4 -command: -command: +command: command: docker-compose up -d +command: command: docker-compose logs From 3de70f8fc6b46085972a4fa9f54f2788e3b1d245 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 20:25:28 +0100 Subject: [PATCH 4/8] Add files via upload modification of project.js --- project.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/project.js b/project.js index 8005e436..89310357 100644 --- a/project.js +++ b/project.js @@ -4,17 +4,24 @@ const express = require('express'); const mysql = require('mysql'); const bodyParser = require('body-parser'); const app = express(); +const host = process.environment.MYSQL_HOST; +const login = process.environment.MYSQL_LOGIN; +const password = process.environment.MYSQL_PASSWORD; +const database = process.environment.MYSQL_DATABASE; +const port = process.environment.MYSQL_PORT; + app.use(bodyParser.urlencoded({ extended: true })); var db = mysql.createConnection({ -host: "localhost", -user: "root", -password: "root", -database: "project", -port: "8889" +host: host, +user: login, +password: password, +database: database, +port: port }); + app.use(function(req, res, next) { if ("key" in req.query) { var key = req.query["key"]; From d2ae887b7a750e257ac5616ca82d1c6a507ecf60 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 23:50:37 +0100 Subject: [PATCH 5/8] Add files via upload --- Dockerfile | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5bf9ed40 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM alpine:latest + +WORKDIR /app + +COPY index.js /app + +RUN apk add nodejs-current nodejs-npm +RUN npm install express +RUN npm install mysql + +EXPOSE 3000 + +CMD node index.js \ No newline at end of file From cc8aa8a076a0cbec402e21a16535de1e4b6591dc Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Sun, 16 Dec 2018 23:54:36 +0100 Subject: [PATCH 6/8] Update answers.md --- answers.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/answers.md b/answers.md index ed606c73..322fe1cb 100644 --- a/answers.md +++ b/answers.md @@ -7,7 +7,7 @@ Firstname: Louis command: sudo docker run app ## 2.3 -question: Ports are not opened +question: Ports are not opened so no access command: sudo docker run -p 3000:3000 -td app ## 2.5 @@ -30,7 +30,14 @@ command: sudo docker ps -a command: sudo docker rename serene_wescoff cont_app ## 2.8 - +question: +Linux 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: command: sudo docker-compose up From 61dd7c670e4082435b2e540a1a695c2f4c8943e1 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Mon, 17 Dec 2018 00:16:05 +0100 Subject: [PATCH 7/8] Add files via upload --- docker-compose.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..1e692371 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: '3' +services: + webService: + ports: + - "3000:3000" + environment: + - MYSQL_HOST=host.docker.internal + - MYSQL_DATABSE=project + - MYSQL_PORT=8889 + - MYSQL_USER=root + - MYSQL_PASSWORD=root + image: louispaban/devops-lab:cont_app \ No newline at end of file From 22cd72dd951ff952c1cf932a2a9769ec5cf26711 Mon Sep 17 00:00:00 2001 From: louispaban <38403898+louispaban@users.noreply.github.com> Date: Mon, 17 Dec 2018 00:25:30 +0100 Subject: [PATCH 8/8] Update Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5bf9ed40..b05c0002 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,7 @@ FROM alpine:latest WORKDIR /app -COPY index.js /app +COPY project.js /app RUN apk add nodejs-current nodejs-npm RUN npm install express @@ -10,4 +10,4 @@ RUN npm install mysql EXPOSE 3000 -CMD node index.js \ No newline at end of file +CMD node project.js