diff --git a/dao/create_tables.sql b/dao/create_tables.sql index ff63026c90be225493865040a34bc98aa9c5a59f..8c190c4da62fc85b6094cabae9ed3a1bfece4f36 100644 --- a/dao/create_tables.sql +++ b/dao/create_tables.sql @@ -1,6 +1,6 @@ -DROP TABLE IF EXISTS person; +DROP TABLE IF EXISTS personTest404; -CREATE TABLE person ( +CREATE TABLE personTest404 ( id int(11) NOT NULL AUTO_INCREMENT, navn varchar(256) NOT NULL, alder int(3) DEFAULT NULL, diff --git a/dao/create_testdata.sql b/dao/create_testdata.sql index b3ee0a7bfc5c6f57c7d77f903919d31c2645e518..dccb9a9462c569ceda9af5a4221de5348ba58580 100644 --- a/dao/create_testdata.sql +++ b/dao/create_testdata.sql @@ -1,3 +1,3 @@ -INSERT INTO person (id, navn, alder, adresse) VALUES - (1, 'Hei Sveisen', 21, 'Gata 1'), - (2, 'Hei Heisen', 22, 'Gata 2'); +INSERT INTO personTest404 (navn, alder, adresse) VALUES + ('Hei Sveisen', 21, 'Gata 1'), + ('Hei Heisen', 22, 'Gata 2'); diff --git a/dao/dao.js b/dao/dao.js index 735891b69078a0cd1690eb8bc87983b0a706dd7e..f5b1f0f47a5bddff25b894a2b1a1b22d01760b1a 100644 --- a/dao/dao.js +++ b/dao/dao.js @@ -8,7 +8,7 @@ module.exports = class Dao { this.pool.getConnection((err, connection) => { console.log("dao: connected to database"); if (err) { - console.log("dao: error connecting"); + console.log("dao: error connecting " + this.pool._freeConnections.length); callback(500, { error: "feil ved ved oppkobling" }); } else { console.log("dao: running sql: " + sql); diff --git a/dao/persondao.js b/dao/persondao.js index 987542b21d3ceef0e2423ce506a08a2323fbcea9..7a318ceb6babda6b402f1ea37b695951aebc9f9f 100644 --- a/dao/persondao.js +++ b/dao/persondao.js @@ -2,12 +2,11 @@ const Dao = require("./dao.js"); module.exports = class PersonDao extends Dao { getAll(callback) { - super.query("select navn, alder, adresse from person", [], callback); + super.query("select navn, alder, adresse from personTest404", [], callback); } getOne(id, callback) { - super.query( - "select navn, alder, adresse from person where id=?", + super.query("select navn, alder, adresse from personTest404 where id=?", [id], callback ); @@ -15,10 +14,39 @@ module.exports = class PersonDao extends Dao { createOne(json, callback) { var val = [json.navn, json.adresse, json.alder]; - super.query( - "insert into person (navn,adresse,alder) values (?,?,?)", + super.query("insert into personTest404 (navn,adresse,alder) values (?,?,?)", val, callback ); } + + deleteOne(id, callback){ + super.query("DELETE FROM personTest404 WHERE id=?", + [id], + callback + ); + } + + updateOne(json, id, callback){ + let navn = json.navn; + let adresse = json.adresse; + let alder = json.alder; + + if (json.navn.trim() === ""){ + navn = super.query("SELECT navn FROM personTest404 WHERE id=?", [id], callback); + } + if (json.adresse.trim() === ""){ + adresse = super.query("SELECT adresse FROM personTest404 WHERE id=?", [id], callback); + } + if(json.alder == null){ + alder = super.query("SELECT alder FROM personTest404 WHERE id=?", [id], callback); + } + + let val = [navn, adresse, alder, id]; + super.query("UPDATE personTest404 SET navn=?, adresse=?, alder=? WHERE id=?", + val, + callback + ); + } + }; diff --git a/dao/persondao.test.js b/dao/persondao.test.js index 79f786bff02a1e925f42f400f2d811203a9188d0..7461b279dcc2483703a6a25e74b2c42a73c99b70 100644 --- a/dao/persondao.test.js +++ b/dao/persondao.test.js @@ -5,17 +5,18 @@ const runsqlfile = require("./runsqlfile.js"); // GitLab CI Pool var pool = mysql.createPool({ - connectionLimit: 1, - host: "mysql", - user: "root", - password: "secret", - database: "supertestdb", - debug: false, - multipleStatements: true + connectionLimit: 1, + host: "mysql.stud.iie.ntnu.no", + user: "nilstesd", + password: "lqqWcMzq", + database: "nilstesd", + debug: false, + multipleStatements: true }); let personDao = new PersonDao(pool); + beforeAll(done => { runsqlfile("dao/create_tables.sql", pool, () => { runsqlfile("dao/create_testdata.sql", pool, done); @@ -77,3 +78,43 @@ test("get all persons from db", done => { personDao.getAll(callback); }); + +test("Deleting person from database", done => { + function callback(status, data) { + console.log("Test callback: status=" + status + ", data=" + JSON.stringify(data)); + expect(data.affectedRows).toBe(1); + length = data.length; + done(); + } + + personDao.deleteOne(2, callback); +}, 30000); + +test("Update person from database", done => { + let oldName; + let oldAge; + let oldAdress; + + personDao.getOne(1, (status, data) => { + oldName = data.navn; + oldAge = data.alder; + oldAdress = data.adresse; + }); + + personDao.updateOne({navn: "Nissen", alder: 100, adresse: "Steder i verden" }, 1, () => {}); + + personDao.getOne("1", (satus, data3) => { + + if(data3.error) data3 = {navn: "", alder: 2, adresse: "asd"}; + + expect(oldName).not.toEqual(data3.navn); + expect(oldAge).not.toEqual(data3.alder); + expect(oldAdress).not.toEqual(data3.adresse); + }); + + + + + done(); + +}); diff --git a/server.js b/server.js index 3e28234bd1eb9a4923da3be7f344f4ecb7ec26e4..b0027a06551346a1cfa15723c50c8464d912ad5b 100644 --- a/server.js +++ b/server.js @@ -7,7 +7,7 @@ app.use(bodyParser.json()); // for å tolke JSON const PersonDao = require("./dao/persondao.js"); var pool = mysql.createPool({ - connectionLimit: 2, + connectionLimit: 10, host: "mysql.stud.iie.ntnu.no", user: "nilstesd", password: "lqqWcMzq", @@ -41,4 +41,20 @@ app.post("/person", (req, res) => { }); }); +app.delete("/person/:personId"), (req, res) => { + console.log("Deleting person :personId"); + personDao.deleteOne(req.params.personId, (status, data) => { + res.status(status); + res.json(data); + }); +} + +app.put("/person/:personId"), (req, res) => { + console.log("Updating person :personId"); + personDao.updateOne(req.body, req.params.personId, (status, data) => { + res.status(status); + res.json(data); + }); +} + var server = app.listen(8080);