From 7d08a9eb3c92b2375a95a06e844cb8b24095b463 Mon Sep 17 00:00:00 2001
From: Brendon <bhps17@inf.ufpr.br>
Date: Mon, 2 Dec 2019 10:16:48 -0300
Subject: [PATCH] delete done

---
 package-lock.json                    | 51 ++++++++++++++++++++++++++++
 package.json                         | 15 ++++++++
 src/controllers/MachineController.js | 46 +++++++++++++++++++++++--
 src/db/query_builder.js              | 27 +++++++++++++++
 src/router.js                        |  3 +-
 5 files changed, 138 insertions(+), 4 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 03c9027..4860cc5 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -430,6 +430,10 @@
     },
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> 19dcf68... POST with image done
 =======
 >>>>>>> 19dcf68... POST with image done
     "buffer-from": {
@@ -438,6 +442,11 @@
       "integrity": "sha512-MQcXEUbCKtEo7bhqEs6560Hyd4XaovZlO/k9V3hjVUF/zwW7KBVdSK4gIt/bzwS9MbR5qob+F5jusZsb0YQK2A=="
     },
 <<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
+>>>>>>> 19dcf68... POST with image done
 =======
 >>>>>>> 6f47ed9... Migration to machine table created
 =======
@@ -796,6 +805,10 @@
     },
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> 19dcf68... POST with image done
 =======
 >>>>>>> 19dcf68... POST with image done
     "concat-stream": {
@@ -803,7 +816,19 @@
       "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.2.tgz",
       "integrity": "sha512-27HBghJxjiZtIk3Ycvn/4kbJk/1uZuJFfuPEns6LaEvpvG1f0hTea8lilrouyo9mVc2GWdcEZ8OLoGmSADlrCw==",
 <<<<<<< HEAD
+<<<<<<< HEAD
+=======
+=======
+>>>>>>> 6f47ed9... Migration to machine table created
 =======
+      "requires": {
+        "buffer-from": "1.1.1",
+        "inherits": "2.0.3",
+        "readable-stream": "2.3.5",
+        "typedarray": "0.0.6"
+      }
+    },
+>>>>>>> 19dcf68... POST with image done
     "config": {
       "version": "3.2.4",
       "resolved": "https://registry.npmjs.org/config/-/config-3.2.4.tgz",
@@ -814,6 +839,7 @@
         "json5": "1.0.1"
 =======
       "requires": {
+<<<<<<< HEAD
         "buffer-from": "1.1.1",
         "inherits": "2.0.3",
         "readable-stream": "2.3.5",
@@ -827,6 +853,8 @@
       "integrity": "sha512-H1XIGfnU1EAkfjSLn9ZvYDRx9lOezDViuzLDgiJ/lMeqjYe3q6iQfpcLt2NInckJgpAeekbNhQkmnnbdEDs9rw==",
       "optional": true,
       "requires": {
+=======
+>>>>>>> 6f47ed9... Migration to machine table created
         "json5": "1.0.1"
       }
     },
@@ -902,11 +930,18 @@
       "version": "1.0.2",
       "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz",
 <<<<<<< HEAD
+<<<<<<< HEAD
 <<<<<<< HEAD
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
 =======
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=",
       "dev": true
+<<<<<<< HEAD
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
+      "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
+>>>>>>> 19dcf68... POST with image done
+=======
 >>>>>>> 6f47ed9... Migration to machine table created
 =======
       "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac="
@@ -6083,11 +6118,18 @@
       "version": "2.0.0",
       "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.0.tgz",
 <<<<<<< HEAD
+<<<<<<< HEAD
 <<<<<<< HEAD
       "integrity": "sha1-o31zL0JxtKsa0HDTVQjoKQeI/6o="
 =======
       "integrity": "sha1-o31zL0JxtKsa0HDTVQjoKQeI/6o=",
       "dev": true
+<<<<<<< HEAD
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
+      "integrity": "sha1-o31zL0JxtKsa0HDTVQjoKQeI/6o="
+>>>>>>> 19dcf68... POST with image done
+=======
 >>>>>>> 6f47ed9... Migration to machine table created
 =======
       "integrity": "sha1-o31zL0JxtKsa0HDTVQjoKQeI/6o="
@@ -7037,6 +7079,10 @@
     },
 <<<<<<< HEAD
 <<<<<<< HEAD
+<<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> 19dcf68... POST with image done
 =======
 >>>>>>> 19dcf68... POST with image done
     "typedarray": {
@@ -7045,6 +7091,11 @@
       "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c="
     },
 <<<<<<< HEAD
+<<<<<<< HEAD
+=======
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
+>>>>>>> 19dcf68... POST with image done
 =======
 >>>>>>> 6f47ed9... Migration to machine table created
 =======
diff --git a/package.json b/package.json
index 7a94ce0..a6b1755 100644
--- a/package.json
+++ b/package.json
@@ -12,7 +12,15 @@
   "scripts": {
     "start": "node src/server.js -c config.js",
     "document": "node documentation.js",
+<<<<<<< HEAD
+<<<<<<< HEAD
     "migrate": "node-pg-migrate"
+=======
+    "migrate" : "node-pg-migrate"
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
+    "migrate": "node-pg-migrate"
+>>>>>>> 19dcf68... POST with image done
   },
   "dependencies": {
     "async": "^2.1.2",
@@ -22,7 +30,14 @@
     "ejs": "^2.5.7",
     "express": "^4.13.3",
     "minimist": "1.2.0",
+<<<<<<< HEAD
+<<<<<<< HEAD
+    "multer": "^1.4.2",
+=======
+>>>>>>> 6f47ed9... Migration to machine table created
+=======
     "multer": "^1.4.2",
+>>>>>>> 19dcf68... POST with image done
     "node-pg-migrate": "^3.23.3",
     "pg": "^6.4.2",
     "pg-copy-streams": "^1.2.0",
diff --git a/src/controllers/MachineController.js b/src/controllers/MachineController.js
index 2b2f722..aa8e6b6 100644
--- a/src/controllers/MachineController.js
+++ b/src/controllers/MachineController.js
@@ -23,7 +23,9 @@
 var qb = require("../db/query_builder.js");
 var db = require("../db/db.js");
 var squel = require("squel").useFlavour('postgres');
-  
+const fs = require('fs');
+const path = require('path');
+
 /**
  * Execute a query on DB and return a response to client
  * If the table specified on query was a table that refers 
@@ -153,7 +155,7 @@ module.exports = {
                     file: filename
                     };
 
-    q = qb.select(table,{"id": req.params['id'] });
+    q = qb.select(table,{"id" : req.params['id']});
 
     db.query(req, q, function(err, data) {
   
@@ -177,5 +179,43 @@ module.exports = {
         return res.status(200).json(machine); 
 
       });
-    }
+    },
+  
+  delete: function(req,res) {
+    const table = "machines";
+    selectQuery = qb.select_id(table,req.params.id);
+    
+    db.query(req, selectQuery, function(err, data) {
+  
+      if (err) {
+        req.log.error(err);
+        return res.status(500).json({error: 'db_connection_failed'});
+      }
+      else if (data.rowCount !== 0) {
+
+        file_to_exclude = data.rows[0].file;
+        const path_to_file = path.join(__dirname, `/../../images/uploads/${file_to_exclude}`);
+          
+        try {
+          fs.unlinkSync(path_to_file);
+        } catch (err) {
+          console.log(err);
+        }
+      }
+     
+    });
+
+    deleteQuery = qb.delete(table, req.params.id);
+
+    db.query(req,deleteQuery, function(err, data) {
+      if (err) {
+        req.log.error(err);
+        return res.status(500).json({error:'db_connection_failed'});
+      }
+      if (data.rowCount === 0) {
+        return res.status(404).json({error:'machine with this id not found'});
+      }
+      return res.status(200).json({status: 'machine deleted with success'});
+    });
+  },
 }
diff --git a/src/db/query_builder.js b/src/db/query_builder.js
index 2830719..db2ff6a 100644
--- a/src/db/query_builder.js
+++ b/src/db/query_builder.js
@@ -119,5 +119,32 @@ module.exports =  {
     var q = squel.update().table(table).where("id = ?", id );
     this.set_from_attr(q, obj);
     return q;
+  },
+   /**
+   * Delete an object in a table
+   * The object must contains the required values to be deleted on table
+   *
+   * @param {String} table Name of table
+   * @param {Number} number Numeric reference to id of tuple 
+   * @returns {Object} Squel query
+   * @public
+   */
+  delete: function (table, id){
+    var q = squel.delete().from(table).where("id = ?", id );
+    return q;
+  },
+
+  /**
+   * Select an object in a table 
+   * The object must contains the required values to be selected on table
+   *
+   * @param {String} table Name of table
+   * @param {Number} number Numeric reference to id of tuple 
+   * @returns {Object} Squel query
+   * @public
+   */
+  select_id: function (table, id){
+    var q = squel.select().from(table).where("id = ?", id );
+    return q;
   }
 };
diff --git a/src/router.js b/src/router.js
index 1b4fe2d..210066d 100644
--- a/src/router.js
+++ b/src/router.js
@@ -152,10 +152,11 @@ module.exports = function(app, config) {
       app.get('/api/data/agentusesyear', function(req, res) {
         data.agentUses(req,res,"agentusesyear");
       });
-
+      
       app.get('/api/machines', MachineController.index);
       app.post('/api/machines',upload.single('file'), MachineController.store);
       app.put('/api/machines/:id',upload.single('file'), MachineController.update);
+      app.delete('/api/machines/:id', MachineController.delete);
     },
 
     /**
-- 
GitLab