From 47110754143f9fa5d1429afcf47202f423ea49d4 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Tue, 4 Dec 2018 11:40:04 +0100 Subject: [PATCH 1/6] add firstname lastname --- answers.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/answers.md b/answers.md index b7025b97..67b7b19c 100644 --- a/answers.md +++ b/answers.md @@ -1,7 +1,7 @@ # Answers -Lastname: -Firstname: +Lastname: Nguyen +Firstname: Mickael ## 2.2 command: From fd9823321b8c40ab0ed96ad5719bd73a75411174 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Tue, 4 Dec 2018 12:05:28 +0100 Subject: [PATCH 2/6] projet zoo --- index.js | 1024 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1024 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 00000000..ff350705 --- /dev/null +++ b/index.js @@ -0,0 +1,1024 @@ +const express = require('express'); +const mysql = require('mysql'); +const bodyParser = require('body-parser'); +const app = express(); + +app.use(bodyParser.urlencoded({ extended: true })); + + +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); + } +}); + +var db = mysql.createConnection({ + host: "localhost", + user: "root", + password: "", + database: "project", + port: "3306" }); + + + +//ANIMALS// +app.get('/animals', function(req, res) { + var query = "SELECT * FROM animals"; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : 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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + //FILTER : 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.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.get('/animals/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM animals WHERE id=" + id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + + 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_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; + + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + +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")); + }); +}); + + + + + +//CAGES// +app.get('/cages', function(req, res) { + var query = "SELECT * FROM cages"; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : 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 += " " + conditions[index] + "='" + req.query[conditions[index]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + //FILTER : 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.post('/cages', function(req, res) { + var id = req.body.id; + 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/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM cages WHERE id=" + id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + 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 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; + + db.query(query, function(err, result, fields) { + 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")); + }); +}); + + + +//FOOD// +app.get('/food', function(req, res) { + var query = "SELECT * FROM food"; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","name","id_animal","quantity"]; + 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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + //FILTER : 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.post('/food', function(req, res) { + var id = req.body.id; + var name = req.body.name; + var id_animal = req.body.id_animal; + var quantity = req.body.quantity; + var query = "INSERT INTO food (name,id_animal,quantity) VALUES ('"+ name + "'," + id_animal + "," + quantity + ")"; + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify("Success")); + }); +}); + + +app.get('/food/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM food WHERE id=" + id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + 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 id_animal = req.body.id_animal; + var quantity = req.body.quantity; + var query = "UPDATE food SET id="+id; + + if("name" in req.body){ + query += ",name = '"+name +"' "; + } + + if("id_animal" in req.body){ + query += ",id_animal = "+id_animal; + } + + if("quantity" in req.body){ + query += ",quantity = "+quantity; + } + + query += " WHERE id="+id; + + db.query(query, function(err, result, fields) { + 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")); + }); +}); + + + + +//STAFF// +app.get('/staff', function(req, res) { + var query = "SELECT * FROM staff"; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + //FILTER : 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.post('/staff', function(req, res) { + var id = req.body.id; + 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/:id', function(req, res) { + var id = req.params.id; + var query = "SELECT * FROM staff WHERE id=" + id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + 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 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; + + db.query(query, function(err, result, fields) { + 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")); + }); +}); + + +//FOOD-STAtS +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)); + }); +}); + + + +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!'); +}); + + +//RELATION ANIMALS/CAGES + +app.get('/cages/:id/animals',function(req,res) { + var id=req.params.id; + + var query= "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date FROM cages INNER JOIN animals ON cages.id = animals.id_cage WHERE cages.id="+id; + + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","name","breed","food_per_day","birthday","entry_date"]; + 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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date", req.query["fields"]); + } + + //FILTER : 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/animals/:id_animal',function(req, res) { + var id =req.params.id; + var id_animal = req.params.id_animal; + var query = "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date FROM cages INNER JOIN animals ON cages.id = animals.id_cage WHERE cages.id=" + id+ " AND animals.id=" + id_animal; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("*", req.query["fields"]); + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); + });  + +app.get('/animals/:id/cages',function(req,res) { + var id=req.params.id; + + var query= "SELECT cages.id, cages.name, cages.description, cages.area FROM animals INNER JOIN cages ON animals.id_cage = cages.id WHERE animals.id="+id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","name","breed","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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("cages.id, cages.name, cages.description, cages.area", req.query["fields"]); + } + + //FILTER : 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/cages/:id_cage',function(req, res) { + var id =req.params.id; + var id_cage = req.params.id_cage; + var query = "SELECT cages.id, cages.name, cages.description, cages.area FROM animals INNER JOIN cages ON animals.id_cage = cages.id WHERE animals.id="+id+ " AND cages.id=" + id_cage; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("cages.id, cages.name, cages.description, cages.area", req.query["fields"]); + } + + db.query(query, function(err, result, fields) { + if (err) throw err; + res.send(JSON.stringify(result)); + }); + });  + + + + + +//RELATION ANIMALS/FOOD + +app.get('/animals/:id/food',function(req,res) { + var id=req.params.id; + + var query= "SELECT food.id, food.name, food.quantity FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id="+id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","name","quantity"]; + 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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("food.id, food.name, food.quantity", req.query["fields"]); + } + + //FILTER : 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/food/:id_food',function(req,res) { + var id=req.params.id; + var id_food = req.params.id_food; + var query= "SELECT food.id, food.name, food.quantity FROM animals INNER JOIN food ON animals.id = food.id_animal WHERE animals.id="+id+ " AND food.id=" + id_food; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("food.id, food.name, food.quantity", req.query["fields"]); + } + + 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.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date FROM food INNER JOIN animals ON food.id_animal = animals.id WHERE food.id="+id; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + + //FILTER : SELECTION + var conditions = ["id","name","breed","food_per_day","birthday","entry_date"]; + 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]] + "'"; + } + } + + //FILTER : 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); + } + + //FILTER : FILTERING + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date", req.query["fields"]); + } + + //FILTER : 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/animals/:id_animal',function(req,res) { + var id=req.params.id; + var id_animal=req.params.id_animal; + var query= "SELECT animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date FROM food INNER JOIN animals ON food.id_animal = animals.id WHERE food.id="+id+ " AND animals.id="+id_animal; + + app.get('/query', function(req, res) { + res.send(JSON.stringify(req.query)); + });  + + if ("fields" in req.query) { + query = query.replace("animals.id, animals.name, animals.breed, animals.food_per_day, animals.birthday, animals.entry_date", req.query["fields"]); + } + + db.query(query,function(err,result,fields){ + if(err) throw err; + res.send(JSON.stringify(result)); + }); + }); + + + + From 02ec9dcff1e14a92cd7abc2b4fdc97b2139b2886 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Sun, 16 Dec 2018 01:40:35 +0100 Subject: [PATCH 3/6] file answers.md updated --- answers.md | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/answers.md b/answers.md index 67b7b19c..48b5746a 100644 --- a/answers.md +++ b/answers.md @@ -4,38 +4,43 @@ Lastname: Nguyen Firstname: Mickael ## 2.2 -command: +command: docker run app ## 2.3 -question: -command: +question: The call fails because ports are not opened +command: docker -i --expose:portNumber //with portNumber = port we open ## 2.5 question: -command: +command: docker login //giving my logs +docker tag appp myNameAccount/devops_lab //with myNameAccount our docker hub ID AND devops_lab the docker hub repository name +docker push myNameAccount/devops_lab ## 2.6 -command: +command:docker system prune -a //delete all images created from the start of the lab on the computer -question: -command: +question: -command: +Using --net=host permits the container to be able to reach localhost on the computer (the conainer having its own network by default) +command: docker run myNameAccount/devops_lab + +command: docker start -d --net=host dreamy_pare //name found in the column "NAMES" and associated to the container previously created ## 2.7 -question: -question: -command: +question: just use the command "docker ps -a" to display all containers and check the column "STATUS" +question: the name of the container is dreamy_pare +command: docker ps -a -command: +command: docker start -d --name newName myNameAccount/devops_lab //with newName the name corresponding to its function ## 2.8 -question: -output: +question: Going into interactive mode : docker run -it myNameAccount/devops_lab /bin/bash , then by using the command given in the lab -> cat /etc/*release, we have the following output (and we can see that the OS from the container is Debian) : + +output: PRETTY_NAME="Debian GNU/Linux 9 (stretch)" NAME="Debian GNU/Linux" VERSION_ID="9" VERSION="9 (stretch)" ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/" ## 3.1 -command: +command: sudo docker-compose up ## 3.4 -command: -command: +command: sudo docker-compose up -d +command: sudo docker-compose logs From 57dc004308fd551e5cb9ea79c3a64e3a4ab0df26 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Sun, 16 Dec 2018 01:47:47 +0100 Subject: [PATCH 4/6] second update answers.md --- answers.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/answers.md b/answers.md index 48b5746a..5046851a 100644 --- a/answers.md +++ b/answers.md @@ -8,10 +8,12 @@ command: docker run app ## 2.3 question: The call fails because ports are not opened + command: docker -i --expose:portNumber //with portNumber = port we open ## 2.5 -question: +question: The reason is that the "docker push" uses the tag to identify the repository where it is supposed to push the image + command: docker login //giving my logs docker tag appp myNameAccount/devops_lab //with myNameAccount our docker hub ID AND devops_lab the docker hub repository name docker push myNameAccount/devops_lab @@ -19,8 +21,7 @@ docker push myNameAccount/devops_lab ## 2.6 command:docker system prune -a //delete all images created from the start of the lab on the computer -question: - +question: Using --net=host permits the container to be able to reach localhost on the computer (the conainer having its own network by default) command: docker run myNameAccount/devops_lab @@ -28,7 +29,9 @@ command: docker start -d --net=host dreamy_pare //name found in the column "NAM ## 2.7 question: just use the command "docker ps -a" to display all containers and check the column "STATUS" + question: the name of the container is dreamy_pare + command: docker ps -a command: docker start -d --name newName myNameAccount/devops_lab //with newName the name corresponding to its function @@ -43,4 +46,5 @@ command: sudo docker-compose up ## 3.4 command: sudo docker-compose up -d + command: sudo docker-compose logs From 9998593720ed69236abecb7d19be8a22a28421d8 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Sun, 16 Dec 2018 01:48:55 +0100 Subject: [PATCH 5/6] third update answers.md --- answers.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/answers.md b/answers.md index 5046851a..e3da4c7f 100644 --- a/answers.md +++ b/answers.md @@ -15,7 +15,9 @@ command: docker -i --expose:portNumber //with portNumber = port we open question: The reason is that the "docker push" uses the tag to identify the repository where it is supposed to push the image command: docker login //giving my logs + docker tag appp myNameAccount/devops_lab //with myNameAccount our docker hub ID AND devops_lab the docker hub repository name + docker push myNameAccount/devops_lab ## 2.6 From 266bd5e5dafe609a04d93f5b8148bee9e5b9f334 Mon Sep 17 00:00:00 2001 From: MickaelNguyen Date: Sun, 16 Dec 2018 01:49:45 +0100 Subject: [PATCH 6/6] final update answers.md --- answers.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/answers.md b/answers.md index e3da4c7f..1ea9f83c 100644 --- a/answers.md +++ b/answers.md @@ -14,7 +14,7 @@ command: docker -i --expose:portNumber //with portNumber = port we open ## 2.5 question: The reason is that the "docker push" uses the tag to identify the repository where it is supposed to push the image -command: docker login //giving my logs +commands: docker login //giving my logs docker tag appp myNameAccount/devops_lab //with myNameAccount our docker hub ID AND devops_lab the docker hub repository name @@ -25,6 +25,7 @@ command:docker system prune -a //delete all images created from the start of the question: Using --net=host permits the container to be able to reach localhost on the computer (the conainer having its own network by default) + command: docker run myNameAccount/devops_lab command: docker start -d --net=host dreamy_pare //name found in the column "NAMES" and associated to the container previously created