From 185378a7e859641e25290863ed5a78197169edbd Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Tue, 4 Dec 2018 12:04:15 +0100 Subject: [PATCH 1/6] Name Update and Adding index --- answers.md | 4 +- index.js | 1019 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1021 insertions(+), 2 deletions(-) create mode 100644 index.js diff --git a/answers.md b/answers.md index b7025b97..732c1233 100644 --- a/answers.md +++ b/answers.md @@ -1,7 +1,7 @@ # Answers -Lastname: -Firstname: +Lastname: Zaafouri +Firstname: Rim ## 2.2 command: diff --git a/index.js b/index.js new file mode 100644 index 00000000..27655a93 --- /dev/null +++ b/index.js @@ -0,0 +1,1019 @@ +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: "zoo", + port: "3306" +}); + +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"); + } +}); +//C - routes for the CREATE +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 birthday = req.body.birthday; + var entry_date = 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_per_day+", '" +birthday+"','"+entry_date+"',"+id_cage+")"; + db.query(query, function (err, result, fields) { + if (err) throw err; + res.send( JSON .stringify( "Success" )); + }); +}); + +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.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.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" )); + }); +}); + + +//R - routes for the READ + +//read animals +app.get( '/animals' , function (req, res) { + var query = "SELECT * FROM animals" + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; +//Selection +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); + } +//Filtering +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)); + }); +}); +//read cages +app.get( '/cages' , function (req, res) { + var query = "SELECT * FROM cages" + var conditions = ["name", "description","area"]; +//Selection +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); + } +//Filtering +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)); + }); +}); +//read food +app.get( '/food' , function (req, res) { + var query = "SELECT * FROM food" + var conditions = ["name", "quantity","id_animal"]; +//Selection +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); + } +//Filtering +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)); + }); +}); +//read staff +app.get( '/staff' , function (req, res) { + var query = "SELECT * FROM staff" + var conditions = ["firstname", "lastname","wage"]; +//Selection +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); + } +//Filtering +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)); + }); +}); + +//R - routes for the Read with parameter + +//read animals with id +app.get( '/animals/:id(\\d+)' , function (req, res) { + var id = req.params.id; + var query = "SELECT * FROM animals WHERE id=" + id; + var conditions = ["name", "breed","food_per_day","birthday","entry_date","id_cage"]; +//Selection + 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); + } +//Filtering + 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)); + }); +}); +//read cages with id +app.get( '/cages/:id(\\d+)' , function (req, res) { + var id = req.params.id; + var query = "SELECT * FROM cages WHERE id=" + id; + var conditions = ["name", "description","area"]; +//Selection + 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); + } +//Filtering + 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)); + }); +}); +//read food with id +app.get( '/food/:id(\\d+)' , function (req, res) { + var id = req.params.id; + var query = "SELECT * FROM food WHERE id=" + id; + var conditions = ["name", "quantity","id_animal"]; +//Selection + 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); + } +//Filtering + 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)); + }); +}); +//read staff with id +app.get( '/staff/:id(\\d+)' , function (req, res) { + var id = req.params.id; + var query = "SELECT * FROM staff WHERE id=" + id; + var conditions = ["firstname", "lastname","wage"]; +//Selection + 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); + } +//Filtering + 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)); + }); +}); + +//U - routes for the update + +//update animal + app.put('/animals/:id',function(req,res) { + console.log(req.params); + var id = req.params.id; + var query = "UPDATE animals SET "; + for (var prop in req.body) { + query+=prop; + query += " = '" + req.body[prop] + "', "; + } + var query = query.substring(0, query.length-2); + query += " WHERE id = " +id; + 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) { + console.log(req.params); + var id = req.params.id; + var query = "UPDATE cages SET "; + for (var prop in req.body) { + query+=prop; + query += " = '" + req.body[prop] + "', "; + } + var query = query.substring(0, query.length-2); + query += " WHERE id = " +id; + 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) { + console.log(req.params); + var id = req.params.id; + var query = "UPDATE food SET "; + for (var prop in req.body) { + query+=prop; + query += " = '" + req.body[prop] + "', "; + } + var query = query.substring(0, query.length-2); + query += " WHERE id = " +id; + 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 query = "UPDATE staff SET "; + for (var prop in req.body) { + query+=prop; + query += " = '" + req.body[prop] + "', "; + } + var query = query.substring(0, query.length-2); + query += " WHERE id = " +id; + db.query(query, function(err,result,fields) { + if(err) throw err; + res.send( JSON.stringify( "Success" )); + }); +}); + +//D - routes for the DELETE + +//delete animals +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 cages +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 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 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" )); + }); +}); + +//D - routes for the delete with parameter + +//delete animals with 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" )); + }); +}); + +//delete cages with 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" )); + }); +}); + +//delete food with 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" )); + }); +}); + +//delete staff with 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" )); + }); +}); + +//Simple food statistics +app.get('/food-stats', function(req, res){ + var query = "SELECT a.id, IF(food_per_day =0,0,quantity/food_per_day) as days_left FROM animals a JOIN food f WHERE a.id = f.id_animal;" + db.query(query, function(err, result, fields){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); +}); + + +//RELATIONSHIPS +//request all data of animal from cage +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; + var conditions = ["name","breed", "food_per_day", "birthday", "entry_date","id_cage"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request specific data of animal from cage +app.get('/cages/:id_cage/animals/:id_animal', function(req, res){ + var id_cage = req.params.id_cage; + var id_animal = req.params.id_animal; + var query = "SELECT animals.* FROM cages INNER JOIN animals ON cages.id = animals.id_cage WHERE cages.id =" + id_cage + " AND animals.id=" + id_animal; + var conditions = ["name","breed", "food_per_day", "birthday", "entry_date","id_cage"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request all data of cages from animal +app.get('/animals/:id/cages', function(req, res){ + var id = req.params.id; + var query = "SELECT cages.* FROM cages INNER JOIN animals ON cages.id = animals.id_cage WHERE animals.id=" + id; + var conditions = ["name","description", "area"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request specific data of cages from animals +app.get('/animals/:id_animal/cages/:id_cage', function(req, res){ + var id_cage = req.params.id_cage; + var id_animal = req.params.id_animal; + var query = "SELECT cages.* FROM cages INNER JOIN animals ON cages.id = animals.id_cage WHERE cages.id =" + id_cage + " AND animals.id=" + id_animal; + var conditions = ["name","description", "area"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request all food from animals +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; + var conditions = ["name","quantity", "id_animal"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request specific food from animal +app.get('/animals/:id_animal/food/:id_food', function(req, res){ + var id_animal = req.params.id_animal; + var id_food = req.params.id_food; + var query = "SELECT food.* FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id =" + id_animal + " AND food.id=" + id_food; + var conditions = ["name","quantity", "id_animal"]; +//Selection +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); +} +//Filtering +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)); + }); +}); + +//request all animals from food +app.get('/food/:id/animals', function(req, res){ + var id = req.params.id; + var query = "SELECT animals.* FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE food.id=" + id; + var conditions = ["name","breed", "food_per_day", "birthday", "entry_date","id_cage"]; +//Selection +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); +} +//Filtering +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('/query', function(req, res){ + res.send(JSON.stringify(req.query)); +}); + +//request specific animal from food +app.get('/food/:id_food/animals/:id_animal', function(req, res){ + var id_animal = req.params.id_animal; + var id_food = req.params.id_food; + var query = "SELECT animals.* FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id =" + id_animal + " AND food.id=" + id_food; + var conditions = ["name","breed", "food_per_day", "birthday", "entry_date","id_cage"]; +//Filters +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); +} +//Filtering +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)); + }); +}); + +//listen to the port +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 4586217f3ca39b696e1aee9d2731dfa62cf5d50e Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Mon, 17 Dec 2018 01:54:31 +0100 Subject: [PATCH 2/6] index.js --- index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 27655a93..02ec93d6 100644 --- a/index.js +++ b/index.js @@ -6,13 +6,14 @@ const app = express(); app.use(bodyParser.urlencoded({ extended: true })); var db = mysql.createConnection({ - host: "localhost" , - user: "root" , - password: "" , - database: "zoo", - port: "3306" + host: process.env.MYSQL_HOST, + user: process.env.MYSQL_USER, + password: process.env.MYSQL_PASSWORD, + database: process.env.MYSQL_DATABASE, + port : process.env.MYSQL_PORT, }); + app.use(function(req, res, next){ if("key" in req.query){ var key = req.query["key"]; From 2fab9de3789ef782a9ba10abc8e9ef93cea24796 Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Mon, 17 Dec 2018 01:58:35 +0100 Subject: [PATCH 3/6] answers.md --- answers.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/answers.md b/answers.md index 732c1233..e7808a1b 100644 --- a/answers.md +++ b/answers.md @@ -4,15 +4,17 @@ Lastname: Zaafouri Firstname: Rim ## 2.2 -command: +command: docker run app ## 2.3 -question: -command: +question: The call fails because the port is not open to access the service +command: docker run -i --expose:3000 app + ## 2.5 question: -command: +command: docker login +docker tag app Desktop/devops-lab ## 2.6 command: From 18439613973cdbabe0b5344aef2804d5c5f4c8d8 Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Mon, 17 Dec 2018 01:59:02 +0100 Subject: [PATCH 4/6] Dockerfile --- Dockerfile | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..5b969a6a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ +FROM node + +WORKDIR /Users/Rim/Desktop/devops-lab + +COPY index.js /Users/Rim/Desktop/devops-lab + +RUN npm install express +RUN npm install mysql + +ENV MYSQL_HOST=host.docker.internal +ENV MYSQL_USER=root +ENV MYSQL_PASSWORD= +ENV MYSQL_DATABASE=nodejs +ENV MYSQL_PORT=3306 + +EXPOSE 3000 + +CMD node index.js From fe8c2ee83e661f68cbf331c13244f9356595449e Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Mon, 17 Dec 2018 02:03:56 +0100 Subject: [PATCH 5/6] oui --- answers.md | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/answers.md b/answers.md index e7808a1b..c5922253 100644 --- a/answers.md +++ b/answers.md @@ -7,37 +7,41 @@ Firstname: Rim command: docker run app ## 2.3 -question: The call fails because the port is not open to access the service -command: docker run -i --expose:3000 app - +question: The call fails because the service cannot access the port. +command: docker run -p 3000:3000 index.js ## 2.5 -question: -command: docker login -docker tag app Desktop/devops-lab +question: We identify the repository using the tag to push the image +command: docker tag index.js rz160211/devops-lab +docker push rz160211/devops-lab ## 2.6 -command: +command: docker system prune -a -question: -command: +question: We start a container +command: docker pull rz160211/devops-lab -command: +command: docker create rz160211/devops-lab docker run rz160211/devops-lab ## 2.7 -question: -question: -command: +question: We retrieve the list of all containers and check that our container is started +question: the name of my container is first_container +command: docker ps -command: +command: docker rename first_container app ## 2.8 -question: -output: +question: The name of the OS of our container is : Alpine 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: docker-compose up ## 3.4 -command: -command: +command: docker-compose up -d +command: docker-compose logs From 9a84022714ea6be0b5d27a6df3d4b702734ddce5 Mon Sep 17 00:00:00 2001 From: Rim <26852077+rz160211@users.noreply.github.com> Date: Mon, 17 Dec 2018 02:05:56 +0100 Subject: [PATCH 6/6] oui --- answers.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/answers.md b/answers.md index c5922253..315a1e28 100644 --- a/answers.md +++ b/answers.md @@ -31,7 +31,7 @@ command: docker ps command: docker rename first_container app ## 2.8 -question: The name of the OS of our container is : Alpine Linux +question: The name of the container is Alpine Linux output: NAME="Alpine Linux" ID=alpine VERSION_ID=3.8.1