From d6774b87b9c6fe07898ac0ee2a1a6530fe342255 Mon Sep 17 00:00:00 2001
From: lisawil <lisawil@stud.ntnu.no>
Date: Thu, 26 Sep 2019 10:30:14 +0200
Subject: [PATCH] fixed

---
 .idea/vcs.xml         |  6 ++++++
 dao/persondao.js      | 17 +++++++++++++++++
 dao/persondao.test.js | 27 +++++++++++++++++++++++++++
 server.js             | 17 +++++++++++++++++
 4 files changed, 67 insertions(+)
 create mode 100644 .idea/vcs.xml

diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..35eb1dd
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="" vcs="Git" />
+  </component>
+</project>
\ No newline at end of file
diff --git a/dao/persondao.js b/dao/persondao.js
index 987542b..55014ec 100644
--- a/dao/persondao.js
+++ b/dao/persondao.js
@@ -13,6 +13,23 @@ module.exports = class PersonDao extends Dao {
     );
   }
 
+  delOne(id, callback) {
+    super.query(
+        "delete from person where id=?",
+        [id],
+        callback
+    );
+  }
+
+  updateOne(json, callback) {
+    let val = [json.navn, json.alder, json.adresse, json.id]
+    super.query(
+        "update person set navn = ?, alder = ?, adresse = ? where id=?",
+        val,
+        callback
+    );
+  }
+
   createOne(json, callback) {
     var val = [json.navn, json.adresse, json.alder];
     super.query(
diff --git a/dao/persondao.test.js b/dao/persondao.test.js
index 79f786b..9515328 100644
--- a/dao/persondao.test.js
+++ b/dao/persondao.test.js
@@ -77,3 +77,30 @@ test("get all persons from db", done => {
 
   personDao.getAll(callback);
 });
+
+test("deletes a row where id== idIn", done => {
+  function callback(status, data) {
+    console.log(
+        "Test callback: status=" + status + ", data.length=" + data.length
+    );
+    expect(data.affectedRows).toBe(1);
+    done();
+  }
+
+  personDao.delOne(1, callback);
+});
+
+test("deletes a row where id== idIn", done => {
+  function callback2(status, data) {
+    expect(data[0].navn).toBe("Heisine Heisen");
+    expect(data[0].adresse).toBe("Gateveien 7");
+    expect(data[0].alder).toBe(35);
+    done();
+  }
+
+  function callback(status, data) {
+    personDao.getOne(2,callback2);
+  }
+
+  personDao.updateOne({navn:"Heisine Heisen", alder:35, adresse:"Gateveien 7", id:2}, callback);
+});
diff --git a/server.js b/server.js
index 3e28234..166b41d 100644
--- a/server.js
+++ b/server.js
@@ -33,6 +33,23 @@ app.get("/person/:personId", (req, res) => {
   });
 });
 
+app.put("/person/:personId", (req, res) => {
+  console.log("/person/:personId: fikk request fra klient");
+  personDao.updateOne(req.body, (status, data) => {
+    res.status(status);
+    res.json(data);
+  });
+});
+
+
+app.delete("/person/:personId", (req, res) => {
+  console.log("/person/:personId: fikk request fra klient");
+  personDao.updateOne(req.params.personId, (status, data) => {
+    res.status(status);
+    res.json(data);
+  });
+});
+
 app.post("/person", (req, res) => {
   console.log("Fikk POST-request fra klienten");
   personDao.createOne(req.body, (status, data) => {
-- 
GitLab