From b667f3c318b56d72d66715a9e3adee37fcb2cafb Mon Sep 17 00:00:00 2001
From: Aleksander Johansen <aleksjoh@stud.ntnu.no>
Date: Mon, 24 Sep 2018 19:21:00 +0200
Subject: [PATCH] Dont read pls

---
 dao/create_tables.sql   |  4 ++--
 dao/create_testdata.sql |  2 +-
 dao/persondao.js        | 38 ++++++++++++++++++++++++++-----
 dao/persondao.test.js   | 50 +++++++++++++++++++++++++++++++++++------
 4 files changed, 79 insertions(+), 15 deletions(-)

diff --git a/dao/create_tables.sql b/dao/create_tables.sql
index ff63026..78d5fd9 100644
--- a/dao/create_tables.sql
+++ b/dao/create_tables.sql
@@ -1,6 +1,6 @@
-DROP TABLE IF EXISTS person;
+DROP TABLE IF EXISTS personDrogas;
 
-CREATE TABLE person  (
+CREATE TABLE personDrogas  (
   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..6b0f784 100644
--- a/dao/create_testdata.sql
+++ b/dao/create_testdata.sql
@@ -1,3 +1,3 @@
-INSERT INTO person (id, navn, alder, adresse) VALUES
+INSERT INTO personDrogas (id, navn, alder, adresse) VALUES
     (1, 'Hei Sveisen', 21, 'Gata 1'),
     (2, 'Hei Heisen', 22, 'Gata 2');
diff --git a/dao/persondao.js b/dao/persondao.js
index 987542b..4580339 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 personDrogas", [], callback);
   }
 
   getOne(id, callback) {
-    super.query(
-      "select navn, alder, adresse from person where id=?",
+    super.query("select navn, alder, adresse from personDrogas 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 personDrogas (navn,adresse,alder) values (?,?,?)",
       val,
       callback
     );
   }
+
+    deleteOne(id, callback){
+        super.query("DELETE FROM personDrogas 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 personDrogas WHERE id=?", [id], callback);
+        }
+        if (json.adresse.trim() === ""){
+            adresse = super.query("SELECT adresse FROM personDrogas WHERE id=?", [id], callback);
+        }
+        if(json.alder == null){
+            alder = super.query("SELECT alder FROM personDrogas WHERE id=?", [id], callback);
+        }
+
+        let val = [navn, adresse, alder, id];
+        super.query("UPDATE personDrogas SET navn=?, adresse=?, alder=? WHERE id=?",
+            val,
+            callback
+        );
+    }
+
 };
diff --git a/dao/persondao.test.js b/dao/persondao.test.js
index 79f786b..ab8943c 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: 10,
+    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,38 @@ 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);
+});
+
+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();
+});
-- 
GitLab