From 0d3bb704953fafef9a66715b810bd0466b1d59b4 Mon Sep 17 00:00:00 2001 From: Sander Nicolausson <sandern@stud.ntnu.no> Date: Mon, 24 Sep 2018 14:01:36 +0200 Subject: [PATCH] fix --- dao/create_tables.sql | 2 ++ dao/persondao.js | 19 +++++++++++++++++++ dao/persondao.test.js | 30 ++++++++++++++++++++++++++++++ package.json | 4 +++- server.js | 16 ++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) diff --git a/dao/create_tables.sql b/dao/create_tables.sql index b707b15..ff63026 100644 --- a/dao/create_tables.sql +++ b/dao/create_tables.sql @@ -1,3 +1,5 @@ +DROP TABLE IF EXISTS person; + CREATE TABLE person ( id int(11) NOT NULL AUTO_INCREMENT, navn varchar(256) NOT NULL, diff --git a/dao/persondao.js b/dao/persondao.js index 987542b..17c07cc 100644 --- a/dao/persondao.js +++ b/dao/persondao.js @@ -21,4 +21,23 @@ module.exports = class PersonDao extends Dao { callback ); } + + updateOne(json, callback){ + var val = [json.navn, json.alder, json.adresse, json.id]; + super.query( + "update person set navn=?, alder=?, adresse=? 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 8c6b6d6..f4ab929 100644 --- a/dao/persondao.test.js +++ b/dao/persondao.test.js @@ -22,6 +22,10 @@ beforeAll(done => { }); }); +afterAll(() =>{ + pool.end(); +}); + test("get one person from db", done => { function callback(status, data) { console.log( @@ -73,3 +77,29 @@ test("get all persons from db", done => { personDao.getAll(callback); }); + +test("delete person from db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data.length=" + data.length + ); + expect(data.length).toBeLessThan(2); + done(); + } + + personDao.deleteOne({id: 1}, callback); +}); + +test("update person in db", done => { + function callback(status, data){ + console.log( + "Test callback: status=" + status + ", data=" + JSON.stringify(data) + ); + expect(data.affectedRows).toBeGreaterThanOrEqual(2); + done(); + } + + personDao.updateOne( + {navn: "Kåre Heia", alder: 100, adresse: "Apalveien 11", id: 2}, callback + ); +}) diff --git a/package.json b/package.json index 763d188..06957bb 100644 --- a/package.json +++ b/package.json @@ -13,10 +13,12 @@ "body-parser": "^1.18.3", "express": "^4.16.3", "fs": "0.0.1-security", - "jest": "^23.6.0", "mysql": "^2.16.0" }, "scripts": { "test": "jest" + }, + "devDependencies": { + "jest": "^23.6.0" } } diff --git a/server.js b/server.js index 3e28234..8c3b21d 100644 --- a/server.js +++ b/server.js @@ -41,4 +41,20 @@ app.post("/person", (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("/perosn/:personId: fikk request fra klient"); + personDao.deleteOne(req.params.personId, (status, data) =>{ + res.status(status); + res.json(data); + }); +}); + var server = app.listen(8080); -- GitLab