From 4626f295b0873c93cc0b8680123182446e627ed6 Mon Sep 17 00:00:00 2001
From: nilstes <nils.tesdal@gmail.com>
Date: Wed, 12 Sep 2018 18:41:46 +0200
Subject: [PATCH] Tester virker

---
 create_tables.sql       |  7 ------
 dao/create_tables.sql   |  8 +++++++
 dao/create_testdata.sql |  3 +++
 dao/dao.js              | 28 ++++++++++++++++++++++
 dao/persondao.js        | 24 +++++++++++++++++++
 dao/persondao.test.js   | 52 +++++++++++++++++++++--------------------
 dao/runsqlfile.js       | 25 ++++++++++++++++++++
 server.js               | 44 ++++++++++++++++++++++++++++++++++
 8 files changed, 159 insertions(+), 32 deletions(-)
 delete mode 100644 create_tables.sql
 create mode 100644 dao/create_tables.sql
 create mode 100644 dao/create_testdata.sql
 create mode 100644 dao/dao.js
 create mode 100644 dao/persondao.js
 create mode 100644 dao/runsqlfile.js
 create mode 100644 server.js

diff --git a/create_tables.sql b/create_tables.sql
deleted file mode 100644
index c56947e..0000000
--- a/create_tables.sql
+++ /dev/null
@@ -1,7 +0,0 @@
-CREATE TABLE`person` (
-  `id` int(11) NOT NULL,
-  `navn` varchar(256) NOT NULL,
-  `alder` int(3) DEFAULT NULL,
-  `adresse` varchar(256) NOT NULL,
-  `bilde_base64` longtext
-) ENGINE=InnoDB DEFAULT CHARSET=latin1;
\ No newline at end of file
diff --git a/dao/create_tables.sql b/dao/create_tables.sql
new file mode 100644
index 0000000..b707b15
--- /dev/null
+++ b/dao/create_tables.sql
@@ -0,0 +1,8 @@
+CREATE TABLE person  (
+  id int(11) NOT NULL AUTO_INCREMENT,
+  navn varchar(256) NOT NULL,
+  alder int(3) DEFAULT NULL,
+  adresse varchar(256) NOT NULL,
+  bilde_base64 longtext,
+  PRIMARY KEY (id)
+) ENGINE=InnoDB DEFAULT CHARSET=latin1;
diff --git a/dao/create_testdata.sql b/dao/create_testdata.sql
new file mode 100644
index 0000000..b3ee0a7
--- /dev/null
+++ b/dao/create_testdata.sql
@@ -0,0 +1,3 @@
+INSERT INTO person (id, navn, alder, adresse) VALUES
+    (1, 'Hei Sveisen', 21, 'Gata 1'),
+    (2, 'Hei Heisen', 22, 'Gata 2');
diff --git a/dao/dao.js b/dao/dao.js
new file mode 100644
index 0000000..735891b
--- /dev/null
+++ b/dao/dao.js
@@ -0,0 +1,28 @@
+module.exports = class Dao {
+  constructor(pool) {
+    // Dependency Injection
+    this.pool = pool;
+  }
+
+  query(sql, params, callback) {
+    this.pool.getConnection((err, connection) => {
+      console.log("dao: connected to database");
+      if (err) {
+        console.log("dao: error connecting");
+        callback(500, { error: "feil ved ved oppkobling" });
+      } else {
+        console.log("dao: running sql: " + sql);
+        connection.query(sql, params, (err, rows) => {
+          connection.release();
+          if (err) {
+            console.log(err);
+            callback(500, { error: "error querying" });
+          } else {
+            console.log("dao: returning rows");
+            callback(200, rows);
+          }
+        });
+      }
+    });
+  }
+};
diff --git a/dao/persondao.js b/dao/persondao.js
new file mode 100644
index 0000000..987542b
--- /dev/null
+++ b/dao/persondao.js
@@ -0,0 +1,24 @@
+const Dao = require("./dao.js");
+
+module.exports = class PersonDao extends Dao {
+  getAll(callback) {
+    super.query("select navn, alder, adresse from person", [], callback);
+  }
+
+  getOne(id, callback) {
+    super.query(
+      "select navn, alder, adresse from person where id=?",
+      [id],
+      callback
+    );
+  }
+
+  createOne(json, callback) {
+    var val = [json.navn, json.adresse, json.alder];
+    super.query(
+      "insert into person (navn,adresse,alder) values (?,?,?)",
+      val,
+      callback
+    );
+  }
+};
diff --git a/dao/persondao.test.js b/dao/persondao.test.js
index dd1f56b..817e168 100644
--- a/dao/persondao.test.js
+++ b/dao/persondao.test.js
@@ -1,5 +1,8 @@
 var mysql = require("mysql");
 
+const PersonDao = require("./persondao.js");
+const runsqlfile = require("./runsqlfile.js");
+
 var pool = mysql.createPool({
   connectionLimit: 1,
   host: "mysql",
@@ -9,31 +12,30 @@ var pool = mysql.createPool({
   debug: false
 });
 
-test("query db", done => {
-  console.log("Connecting to database");
-  
-  function callback(data) {
-    console.log("Callback: " + data);
-    expect(data).toBe("SUCCESS");
+let personDao = new PersonDao(pool);
+
+beforeAll(done => {
+  runsqlfile("dao/create_tables.sql", pool, () => {
+    runsqlfile("dao/create_testdata.sql", pool, done);
+  });
+});
+
+test("get one person from db", done => {
+  function callback(status, data) {
+    console.log("Callback: status=" + status + ", data=" + data);
+    expect(data[0].navn).toBe("Hei Sveisen");
     done();
   }
 
-  pool.getConnection((err, connection) => {
-    console.log("Connected to database");
-    if (err) {
-      callback("Feil ved kobling til databasen: " + err);
-    } else {
-      connection.query(
-        "select 1",
-        (err, rows) => {
-          connection.release();
-          if (err) {
-            callback("Query error: " + err);
-          } else {
-            callback("SUCCESS");
-          }
-        }
-      );
-    }
-  });
-});
\ No newline at end of file
+  personDao.getOne(1, callback);
+});
+
+test("get all persons from db", done => {
+  function callback(status, data) {
+    console.log("Callback: status=" + status + ", data=" + data);
+    expect(data.length).toBe(2);
+    done();
+  }
+
+  personDao.getAll(callback);
+});
diff --git a/dao/runsqlfile.js b/dao/runsqlfile.js
new file mode 100644
index 0000000..0144e2f
--- /dev/null
+++ b/dao/runsqlfile.js
@@ -0,0 +1,25 @@
+var mysql = require("mysql");
+var fs = require("fs");
+
+module.exports = function run(filename, pool, done) {
+  console.log("runsqlfile: reading file " + filename);
+  let sql = fs.readFileSync(filename, "utf8");
+  pool.getConnection((err, connection) => {
+    if (err) {
+      console.log("runsqlfile: error connecting");
+      done();
+    } else {
+      console.log("runsqlfile: connected");
+      connection.query(sql, (err, rows) => {
+        connection.release();
+        if (err) {
+          console.log(err);
+          done();
+        } else {
+          console.log("runsqlfile: run ok");
+          done();
+        }
+      });
+    }
+  });
+};
diff --git a/server.js b/server.js
new file mode 100644
index 0000000..22ebb02
--- /dev/null
+++ b/server.js
@@ -0,0 +1,44 @@
+var express = require("express");
+var mysql = require("mysql");
+var bodyParser = require("body-parser");
+var app = express();
+var apiRoutes = express.Router();
+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
+});
+
+let personDao = new PersonDao(pool);
+
+app.get("/person", (req, res) => {
+  console.log("/person: fikk request fra klient");
+  personDao.getAll((status, json) => {
+    res.status(status);
+    res.json(json);
+  });
+});
+
+app.get("/person/:personId", (req, res) => {
+  console.log("/person/:personId: fikk request fra klient");
+  personDao.getOne(req.params.personId, (status, json) => {
+    res.status(status);
+    res.json(json);
+  });
+});
+
+app.post("/person", (req, res) => {
+  console.log("Fikk POST-request fra klienten");
+  personDao.createOne(req.body, (status, json) => {
+    res.status(status);
+    res.json(json);
+  });
+});
+
+var server = app.listen(8080);
-- 
GitLab