diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..35eb1ddfbbc029bcab630581847471d7f238ec53 --- /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 987542b21d3ceef0e2423ce506a08a2323fbcea9..55014ec4029a0f94c8e2df2da75be7276e8e015e 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 79f786bff02a1e925f42f400f2d811203a9188d0..9515328051c84429c7adc7869be95c7be7393d9c 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 3e28234bd1eb9a4923da3be7f344f4ecb7ec26e4..166b41d2e1b77c95832a1588c367e134b6c2b9f3 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) => {