From 1b842fa1c447e3c528c2580089c1f2b8afb3a84c Mon Sep 17 00:00:00 2001 From: Jbt <5770yr@gmail.com> Date: Sun, 5 Nov 2017 10:24:23 +0200 Subject: [PATCH 1/3] for sakk --- server.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 537c9b0..24aac5c 100644 --- a/server.js +++ b/server.js @@ -24,11 +24,14 @@ app.route('/heroes') .post(function (req, res) { heroes.push(req.body); res.send(heroes); + }) + .delete(function (req, res) { + heroes.splice(heroes.findIndex((hero) => hero.name === req.query.name), 1); + res.send(heroes); }); app.route('/heroes/:id') .get(function (req, res) { - /* heroes.find((hero) => hero.id == req.params.id);*/ var retHeroes = heroes.find(function(hero){ return hero.id === req.params.id; }); From 2d50235ee73e12ba775404a91310111e7ce196ec Mon Sep 17 00:00:00 2001 From: Jbt <5770yr@gmail.com> Date: Sun, 5 Nov 2017 16:50:44 +0200 Subject: [PATCH 2/3] for sakk --- public/db.js | 7 +++++++ public/logic.js | 24 ++++++++++++++++++++++++ server.js | 28 +++++++++------------------- 3 files changed, 40 insertions(+), 19 deletions(-) create mode 100644 public/db.js create mode 100644 public/logic.js diff --git a/public/db.js b/public/db.js new file mode 100644 index 0000000..0fef13c --- /dev/null +++ b/public/db.js @@ -0,0 +1,7 @@ +var heroes = [ + {"id": "1", "name":"heroes1"}, + {"id": "2", "name":"heroes2"}, + {"id": "3", "name":"heroes3"} +]; + +module.exports = heroes; diff --git a/public/logic.js b/public/logic.js new file mode 100644 index 0000000..d76665e --- /dev/null +++ b/public/logic.js @@ -0,0 +1,24 @@ +var heroes = require("./db"); + +class heroeLogic { + getAllHeroes(){ + return heroes; + } + getOneHeroes(id){ + return heroes.find((heroe) => heroe.id === id); + } + addHeroe(id, name){ + heroes.push({id:name}); + return heroes; + } + editHeroe(id, name){ + heroes.find((heroe) => heroe.id === id).name = name; + return heroes; + } + deletHeroe(id){ + heroes.splice(heroes.findIndex((heroe) => heroe.id === id), 1); + return heroes; + } +} + +module.exports = heroeLogic; \ No newline at end of file diff --git a/server.js b/server.js index 24aac5c..a6f28a2 100644 --- a/server.js +++ b/server.js @@ -6,42 +6,32 @@ app.use(express.static(path.join(__dirname, 'public'))); var bodyParser = require('body-parser') app.use(bodyParser.json()); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({extended: true})); // to support URL-encoded bodies - -var heroes = [ - {"id": "1", "name":"heroes1"}, - {"id": "2", "name":"heroes2"}, - {"id": "3", "name":"heroes3"} -]; +var heroes = require("./public/logic"); http.listen(3000, function(){ console.log('listening on *:3000'); }); +var logic = new heroes(); + app.route('/heroes') .get(function (req, res) { - res.send(heroes); + res.send(logic.getAllHeroes()); }) .post(function (req, res) { - heroes.push(req.body); - res.send(heroes); + res.send(logic.addHeroe(req.body.id, req.body.name)); }) .delete(function (req, res) { - heroes.splice(heroes.findIndex((hero) => hero.name === req.query.name), 1); - res.send(heroes); + res.send(logic.deletHeroe(req.query.name)); }); app.route('/heroes/:id') .get(function (req, res) { - var retHeroes = heroes.find(function(hero){ - return hero.id === req.params.id; - }); - res.send(retHeroes); + res.send(logic.getOneHeroes(req.params.id)); }) .put(function (req, res) { - heroes.find((hero) => hero.id === req.params.id).name = req.query.name; - res.send(heroes); + res.send(logic.editHeroe(req.params.id, req.query.name)); }) .delete(function (req, res) { - heroes.splice(heroes.findIndex((hero) => hero.id === req.params.id), 1); - res.send(heroes); + res.send(logic.deletHeroe(req.params.id)); }); From a84987569bf8df61932d6f631e33e8375107c0d1 Mon Sep 17 00:00:00 2001 From: Jbt <5770yr@gmail.com> Date: Mon, 6 Nov 2017 16:52:55 +0200 Subject: [PATCH 3/3] testing not working --- public/logic.js | 6 +++++- server.js | 9 ++++----- test/test.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 6 deletions(-) create mode 100644 test/test.js diff --git a/public/logic.js b/public/logic.js index d76665e..be04755 100644 --- a/public/logic.js +++ b/public/logic.js @@ -15,10 +15,14 @@ class heroeLogic { heroes.find((heroe) => heroe.id === id).name = name; return heroes; } - deletHeroe(id){ + deletById(id){ heroes.splice(heroes.findIndex((heroe) => heroe.id === id), 1); return heroes; } + deletByName(name){ + heroes.splice(heroes.findIndex((heroe) => heroe.name === name), 1); + return heroes; + } } module.exports = heroeLogic; \ No newline at end of file diff --git a/server.js b/server.js index a6f28a2..8f850f3 100644 --- a/server.js +++ b/server.js @@ -6,14 +6,13 @@ app.use(express.static(path.join(__dirname, 'public'))); var bodyParser = require('body-parser') app.use(bodyParser.json()); // to support JSON-encoded bodies app.use(bodyParser.urlencoded({extended: true})); // to support URL-encoded bodies -var heroes = require("./public/logic"); +var logicFile = require("./public/logic"); +var logic = new logicFile(); http.listen(3000, function(){ console.log('listening on *:3000'); }); -var logic = new heroes(); - app.route('/heroes') .get(function (req, res) { res.send(logic.getAllHeroes()); @@ -22,7 +21,7 @@ app.route('/heroes') res.send(logic.addHeroe(req.body.id, req.body.name)); }) .delete(function (req, res) { - res.send(logic.deletHeroe(req.query.name)); + res.send(logic.deletByName(req.query.name)); }); app.route('/heroes/:id') @@ -33,5 +32,5 @@ app.route('/heroes/:id') res.send(logic.editHeroe(req.params.id, req.query.name)); }) .delete(function (req, res) { - res.send(logic.deletHeroe(req.params.id)); + res.send(logic.deletById(req.params.id)); }); diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..a78bd5a --- /dev/null +++ b/test/test.js @@ -0,0 +1,36 @@ +var expect = require("chai").expect; +var db = require("../public/db"); +var logicFile = require("../public/logic"); +var logic = new logicFile(); + +describe("Get Heroes", function() { + describe("Get All Heroes", function() { + it("Get All Heroes", function() { + var allHeroes = logic.getAllHeroes(); + + expect(allHeroes).to.equal(db); + }); + }); + + describe("Get one Heroe", function() { + it("Get one Heroe", function() { + let id = '2'; + let byId = logic.getOneHeroes(id); + + expect(byId).to.deep.equal(db.find((heroe) => heroe.id === id)); + }); + }); + + describe("Delete Heroe", function() { + it("Delete Heroe", function() { + let id = '2'; + let name = 'heroes3'; + + let byId = logic.deletById(id); + let byName = logic.deletByName(name) + + expect(byId.find((heroe) => heroe.id === id)).to.deep.equal(-1); + expect(byName.find((heroe) => heroe.id === name)).to.deep.equal(-1); + }); + }); +});