From bd3d6719d9881950b99c6d0ef6becd711c065e46 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Sveinung=20=C3=98verland?= <sveinungg.overlandd@gmail.com>
Date: Mon, 24 Sep 2018 19:07:02 +0200
Subject: [PATCH] This might brake everything

---
 dao/create_tables.sql   |  4 +--
 dao/create_testdata.sql |  6 ++---
 dao/dao.js              |  2 +-
 dao/persondao.js        | 38 ++++++++++++++++++++++++----
 dao/persondao.test.js   | 55 +++++++++++++++++++++++++++++++++++------
 server.js               | 18 +++++++++++++-
 6 files changed, 104 insertions(+), 19 deletions(-)

diff --git a/dao/create_tables.sql b/dao/create_tables.sql
index ff63026..8c190c4 100644
--- a/dao/create_tables.sql
+++ b/dao/create_tables.sql
@@ -1,6 +1,6 @@
-DROP TABLE IF EXISTS person;
+DROP TABLE IF EXISTS personTest404;
 
-CREATE TABLE person  (
+CREATE TABLE personTest404  (
   id int(11) NOT NULL AUTO_INCREMENT,
   navn varchar(256) NOT NULL,
   alder int(3) DEFAULT NULL,
diff --git a/dao/create_testdata.sql b/dao/create_testdata.sql
index b3ee0a7..dccb9a9 100644
--- a/dao/create_testdata.sql
+++ b/dao/create_testdata.sql
@@ -1,3 +1,3 @@
-INSERT INTO person (id, navn, alder, adresse) VALUES
-    (1, 'Hei Sveisen', 21, 'Gata 1'),
-    (2, 'Hei Heisen', 22, 'Gata 2');
+INSERT INTO personTest404 (navn, alder, adresse) VALUES
+    ('Hei Sveisen', 21, 'Gata 1'),
+    ('Hei Heisen', 22, 'Gata 2');
diff --git a/dao/dao.js b/dao/dao.js
index 735891b..f5b1f0f 100644
--- a/dao/dao.js
+++ b/dao/dao.js
@@ -8,7 +8,7 @@ module.exports = class Dao {
     this.pool.getConnection((err, connection) => {
       console.log("dao: connected to database");
       if (err) {
-        console.log("dao: error connecting");
+        console.log("dao: error connecting " + this.pool._freeConnections.length);
         callback(500, { error: "feil ved ved oppkobling" });
       } else {
         console.log("dao: running sql: " + sql);
diff --git a/dao/persondao.js b/dao/persondao.js
index 987542b..7a318ce 100644
--- a/dao/persondao.js
+++ b/dao/persondao.js
@@ -2,12 +2,11 @@ const Dao = require("./dao.js");
 
 module.exports = class PersonDao extends Dao {
   getAll(callback) {
-    super.query("select navn, alder, adresse from person", [], callback);
+    super.query("select navn, alder, adresse from personTest404", [], callback);
   }
 
   getOne(id, callback) {
-    super.query(
-      "select navn, alder, adresse from person where id=?",
+    super.query("select navn, alder, adresse from personTest404 where id=?",
       [id],
       callback
     );
@@ -15,10 +14,39 @@ module.exports = class PersonDao extends Dao {
 
   createOne(json, callback) {
     var val = [json.navn, json.adresse, json.alder];
-    super.query(
-      "insert into person (navn,adresse,alder) values (?,?,?)",
+    super.query("insert into personTest404 (navn,adresse,alder) values (?,?,?)",
       val,
       callback
     );
   }
+
+    deleteOne(id, callback){
+        super.query("DELETE FROM personTest404 WHERE id=?",
+            [id],
+            callback
+        );
+    }
+
+    updateOne(json, id, callback){
+        let navn = json.navn;
+        let adresse = json.adresse;
+        let alder = json.alder;
+
+        if (json.navn.trim() === ""){
+            navn = super.query("SELECT navn FROM personTest404 WHERE id=?", [id], callback);
+        }
+        if (json.adresse.trim() === ""){
+            adresse = super.query("SELECT adresse FROM personTest404 WHERE id=?", [id], callback);
+        }
+        if(json.alder == null){
+            alder = super.query("SELECT alder FROM personTest404 WHERE id=?", [id], callback);
+        }
+
+        let val = [navn, adresse, alder, id];
+        super.query("UPDATE personTest404 SET navn=?, adresse=?, alder=? WHERE id=?",
+            val,
+            callback
+        );
+    }
+
 };
diff --git a/dao/persondao.test.js b/dao/persondao.test.js
index 79f786b..7461b27 100644
--- a/dao/persondao.test.js
+++ b/dao/persondao.test.js
@@ -5,17 +5,18 @@ const runsqlfile = require("./runsqlfile.js");
 
 // GitLab CI Pool
 var pool = mysql.createPool({
-  connectionLimit: 1,
-  host: "mysql",
-  user: "root",
-  password: "secret",
-  database: "supertestdb",
-  debug: false,
-  multipleStatements: true
+    connectionLimit: 1,
+    host: "mysql.stud.iie.ntnu.no",
+    user: "nilstesd",
+    password: "lqqWcMzq",
+    database: "nilstesd",
+    debug: false,
+    multipleStatements: true
 });
 
 let personDao = new PersonDao(pool);
 
+
 beforeAll(done => {
   runsqlfile("dao/create_tables.sql", pool, () => {
     runsqlfile("dao/create_testdata.sql", pool, done);
@@ -77,3 +78,43 @@ test("get all persons from db", done => {
 
   personDao.getAll(callback);
 });
+
+test("Deleting person from database", done => {
+    function callback(status, data) {
+        console.log("Test callback: status=" + status + ", data=" + JSON.stringify(data));
+        expect(data.affectedRows).toBe(1);
+        length = data.length;
+        done();
+    }
+
+    personDao.deleteOne(2, callback);
+}, 30000);
+
+test("Update person from database", done => {
+    let oldName;
+    let oldAge;
+    let oldAdress;
+
+    personDao.getOne(1, (status, data) => {
+        oldName = data.navn;
+        oldAge = data.alder;
+        oldAdress = data.adresse;
+    });
+
+    personDao.updateOne({navn: "Nissen", alder: 100, adresse: "Steder i verden" }, 1, () => {});
+
+    personDao.getOne("1", (satus, data3) => {
+
+        if(data3.error) data3 = {navn: "", alder: 2, adresse: "asd"};
+
+        expect(oldName).not.toEqual(data3.navn);
+        expect(oldAge).not.toEqual(data3.alder);
+        expect(oldAdress).not.toEqual(data3.adresse);
+    });
+
+
+
+
+    done();
+
+});
diff --git a/server.js b/server.js
index 3e28234..b0027a0 100644
--- a/server.js
+++ b/server.js
@@ -7,7 +7,7 @@ app.use(bodyParser.json()); // for å tolke JSON
 const PersonDao = require("./dao/persondao.js");
 
 var pool = mysql.createPool({
-  connectionLimit: 2,
+  connectionLimit: 10,
   host: "mysql.stud.iie.ntnu.no",
   user: "nilstesd",
   password: "lqqWcMzq",
@@ -41,4 +41,20 @@ app.post("/person", (req, res) => {
   });
 });
 
+app.delete("/person/:personId"), (req, res) => {
+    console.log("Deleting person :personId");
+    personDao.deleteOne(req.params.personId, (status, data) => {
+        res.status(status);
+        res.json(data);
+    });
+}
+
+app.put("/person/:personId"), (req, res) => {
+    console.log("Updating person :personId");
+    personDao.updateOne(req.body, req.params.personId, (status, data) => {
+        res.status(status);
+        res.json(data);
+    });
+}
+
 var server = app.listen(8080);
-- 
GitLab