Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions public/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var heroes = [
{"id": "1", "name":"heroes1"},
{"id": "2", "name":"heroes2"},
{"id": "3", "name":"heroes3"}
];

module.exports = heroes;
28 changes: 28 additions & 0 deletions public/logic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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;
}
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;
28 changes: 10 additions & 18 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,31 @@ 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 logicFile = require("./public/logic");
var logic = new logicFile();

http.listen(3000, function(){
console.log('listening on *:3000');
});

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) {
res.send(logic.deletByName(req.query.name));
});

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;
});
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.deletById(req.params.id));
});
36 changes: 36 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var expect = require("chai").expect;
var db = require("../public/db");
var logicFile = require("../public/logic");
var logic = new logicFile();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use let/const
logic/logicFile - not good names


describe("Get Heroes", function() {
describe("Get All Heroes", function() {
it("Get All Heroes", function() {
var allHeroes = logic.getAllHeroes();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u should test your server functionality using chai


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);
});
});
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u didn't check all of your functionality, like add hero