diff --git a/dao/create_testdata.sql b/dao/create_testdata.sql index b3ee0a7bfc5c6f57c7d77f903919d31c2645e518..420d2cd0098f0ae9b2facd091fc98ba846d05fc7 100644 --- a/dao/create_testdata.sql +++ b/dao/create_testdata.sql @@ -1,3 +1,6 @@ INSERT INTO person (id, navn, alder, adresse) VALUES (1, 'Hei Sveisen', 21, 'Gata 1'), - (2, 'Hei Heisen', 22, 'Gata 2'); + (2, 'Hei Leisen', 21, 'Gata 2'), + (3, 'Hei Meisen', 21, 'Gata 3'), + (4, 'Hei Peisen', 21, 'Gata 4'), + (5, 'Hei Heisen', 22, 'Gata 5'); diff --git a/dao/persondao.js b/dao/persondao.js index 987542b21d3ceef0e2423ce506a08a2323fbcea9..454830cb76057292504c40a6b36e1ea9ac21cafb 100644 --- a/dao/persondao.js +++ b/dao/persondao.js @@ -13,6 +13,24 @@ module.exports = class PersonDao extends Dao { ); } + updateOne(json, id, callback) { + var val = [json.navn, json.adresse, json.alder, id]; + super.query( + "UPDATE person SET navn=?, adresse=?, alder=? WHERE id=?", + val, + callback + ); + } + + deleteOne(id, callback) { + super.query( + "DELETE FROM person WHERE id=?;", + [id], + callback + ); + } + + createOne(json, callback) { var val = [json.navn, json.adresse, json.alder]; super.query( @@ -21,4 +39,4 @@ module.exports = class PersonDao extends Dao { callback ); } -}; +} diff --git a/dao/persondao.test.js b/dao/persondao.test.js index 79f786bff02a1e925f42f400f2d811203a9188d0..b3db126dbfd8826696632f74458833e5ac69ec00 100644 --- a/dao/persondao.test.js +++ b/dao/persondao.test.js @@ -77,3 +77,44 @@ test("get all persons from db", done => { personDao.getAll(callback); }); + +test("delete one person from db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data.length=" + data.length + ); + expect(data.affectedRows).toBeGreaterThanOrEqual(1); + done(); + } + + personDao.deleteOne(1, callback); +}); + +test("get all persons from db after delete", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data.length=" + data.length + ); + expect(data.length).toBe(5); + done(); + } + + personDao.getAll(callback); +}); + +test("update one person from db", done => { + function callback(status, data) { + console.log( + "Test callback: status=" + status + ", data=" + JSON.stringify(data) + ); + expect(data.affectedRows).toBeGreaterThanOrEqual(1); + done(); + } + + personDao.updateOne( + { navn: "Billy Bob", adresse: "Gata 3", alder: "34"}, + {id:"1"}, + callback + ); +}); + diff --git a/server.js b/server.js index 3e28234bd1eb9a4923da3be7f344f4ecb7ec26e4..48c3eefa63ebb94ed9196f7432310dde55fc1b47 100644 --- a/server.js +++ b/server.js @@ -41,4 +41,20 @@ app.post("/person", (req, res) => { }); }); +pp.delete("/person/:personId", (req, res) => { + console.log("/person/:personId: fikk delete-request fra klient"); + personDao.deleteOne(req.params.personId, (status, data) => { + res.status(status); + res.json(data); + }); +}); + +app.put("/person/:personId", (req, res) => { + console.log("Fikk put-request fra klienten"); + personDao.updateOne(req.body, req.params.personId, (status, data) => { + res.status(status); + res.json(data); + }); +}); + var server = app.listen(8080);