diff --git a/dao/create_tables.sql b/dao/create_tables.sql index b707b15a42fd373ef7cdfc1094b4b5ed3df2060d..ff63026c90be225493865040a34bc98aa9c5a59f 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 987542b21d3ceef0e2423ce506a08a2323fbcea9..17c07cc8425ed0edaabc1977c67eef94c89eb23c 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 8c6b6d634b66fb0e7fe2e351a52e33f00754feac..f4ab9299d7abba08afe1ea4a0a47a1227cae20d9 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 763d18837f955a543d60041017b6c3238ec71d33..06957bb403d96177cf984b2a15d2fe438eb6eba9 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 3e28234bd1eb9a4923da3be7f344f4ecb7ec26e4..8c3b21d85602363b2d373e10ed7fef054a78243b 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);