From 8c3d11cc6f9f79f3e133167a8e7b153b94f7b290 Mon Sep 17 00:00:00 2001
From: Magnus Revheim Martinsen <magnusrm.stud.ntnu.no>
Date: Mon, 24 Sep 2018 21:14:08 +0200
Subject: [PATCH] update, delete + tests

---
 dao/persondao.js      | 17 +++++++++++++++++
 dao/persondao.test.js | 35 +++++++++++++++++++++++++++++++----
 server.js             | 28 ++++++++++++++++++++++------
 3 files changed, 70 insertions(+), 10 deletions(-)

diff --git a/dao/persondao.js b/dao/persondao.js
index 987542b..a2311b0 100644
--- a/dao/persondao.js
+++ b/dao/persondao.js
@@ -21,4 +21,21 @@ module.exports = class PersonDao extends Dao {
       callback
     );
   }
+
+  updateOne(id, json, callback) {
+    var val = [json.navn, json.adresse, json.alder, id];
+    super.query(
+        "update person set navn=?, adresse=?, alder=? where id=?",
+        val,
+        callback
+    );
+  }
+
+  deleteOne(id, callback) {
+    super.query(
+        "delete from person where id=?",
+        [id],
+        callback
+    );
+  }
 };
diff --git a/dao/persondao.test.js b/dao/persondao.test.js
index 79f786b..594cb4a 100644
--- a/dao/persondao.test.js
+++ b/dao/persondao.test.js
@@ -6,10 +6,10 @@ const runsqlfile = require("./runsqlfile.js");
 // GitLab CI Pool
 var pool = mysql.createPool({
   connectionLimit: 1,
-  host: "mysql",
-  user: "root",
-  password: "secret",
-  database: "supertestdb",
+  host: "mysql.stud.iie.ntnu.no",
+  user: "magnusrm",
+  password: "fKzwPFN3",
+  database: "magnusrm",
   debug: false,
   multipleStatements: true
 });
@@ -77,3 +77,30 @@ test("get all persons from db", done => {
 
   personDao.getAll(callback);
 });
+
+test("update a persons data to db", done => {
+    function callback(status, data) {
+        console.log(
+            "Test callback: status=" + status + ", data.length=" + data.length
+        );
+        expect(data.affectedRows).toBe(1);
+        done();
+    }
+    personDao.updateOne(
+        3,
+        { navn: "per", adresse: "ila 5", alder: 23},
+        callback);
+});
+
+test("delete a person in db", done => {
+    function callback(status, data) {
+        console.log(
+            "Test callback: status=" + status + ", data.length=" + data.length
+        );
+        expect(data.affectedRows).toBe(1);
+        done();
+    }
+    personDao.deleteOne(
+        1,
+        callback);
+});
diff --git a/server.js b/server.js
index 3e28234..14c2b44 100644
--- a/server.js
+++ b/server.js
@@ -7,12 +7,12 @@ app.use(bodyParser.json()); // for å tolke JSON
 const PersonDao = require("./dao/persondao.js");
 
 var pool = mysql.createPool({
-  connectionLimit: 2,
-  host: "mysql.stud.iie.ntnu.no",
-  user: "nilstesd",
-  password: "lqqWcMzq",
-  database: "nilstesd",
-  debug: false
+    connectionLimit: 2,
+    host: "mysql.stud.iie.ntnu.no",
+    user: "magnusrm",
+    password: "fKzwPFN3",
+    database: "magnusrm",
+    debug: false
 });
 
 let personDao = new PersonDao(pool);
@@ -41,4 +41,20 @@ app.post("/person", (req, res) => {
   });
 });
 
+app.post("/person/:personId", (req, res) => {
+  console.log("Fikk POST-request fra klientetn, update");
+  personDao.updateOne(req.params.personId, req.body, (status, data) => {
+    res.status(status);
+    res.json(data);
+  });
+});
+
+app.post("/person/:personId", (req, res) => {
+    console.log("Fikk POST-request fra klienten, delete");
+    personDao.deleteOne(req.params.personId, (status, data) => {
+        res.status(status);
+        res.json(data);
+    });
+});
+
 var server = app.listen(8080);
-- 
GitLab