Created
May 22, 2016 18:50
-
-
Save badnorseman/7a9b4daa11c56dfd1caf1f9a03f4980d to your computer and use it in GitHub Desktop.
An OOP example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const express = require('express') | |
const bodyParser = require('body-parser') | |
const todos = require('./todos') | |
require('./env') | |
const app = express() | |
app.use(bodyParser.json()) | |
app.use(express.static('public')) | |
app.get('/todos', (req, res) => todos.retrieveAll(req, res)) | |
app.post('/todos', (req, res) => todos.create(req, res)) | |
app.put('/todos/:id', (req, res) => todos.update(req, res)) | |
app.delete('/todos/:id', (req, res) => todos.delete(req, res)) | |
const port = process.env.PORT || 9000 | |
app.listen(port, () => console.log(`listening on ${port}`)) | |
module.exports = app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const Todos = require('./todos.storage') | |
exports.create = (req, res) => { | |
if (!req.body || !req.body.text) return res.sendStatus(400) | |
const text = req.body.text | |
const todo = Todos.create(text) | |
res.status(201).json(todo) | |
} | |
exports.retrieveAll = (req, res) => { | |
const todos = Todos.retrieveAll() | |
res.status(200).json(todos) | |
} | |
exports.update = (req, res) => { | |
if (!req.params || !req.params.id || !req.body || !req.body.text) return res.sendStatus(400) | |
const id = req.params.id | |
const text = req.body.text | |
const todo = Todos.update(id, text) | |
res.status(200).json(todo) | |
} | |
exports.delete = (req, res) => { | |
if (!req.params || !req.params.id) return res.sendStatus(400) | |
const id = req.params.id | |
const todo = Todos.delete(id) | |
res.status(200).json(todo) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
let Storage = function() { | |
this.items = {} | |
this.id = 0 | |
} | |
Storage.prototype.create = function(text) { | |
const id = this.id | |
const item = { text, id } | |
this.items = Object.assign({}, this.items, { [id]: item }) | |
this.id += 1 | |
return item | |
} | |
Storage.prototype.retrieveAll = function() { | |
return this.items | |
} | |
Storage.prototype.update = function(id, text) { | |
if (isNaN(id) || !text) return 'not valid' | |
let item | |
if (this.items[id]) { | |
item = this.items[id] | |
item.text = text | |
this.items = Object.assign({}, this.items, | |
Object.keys(this.items).reduce((result, key) => { | |
if (key === id) { result[key] = item } | |
return result | |
}, {})) | |
} else { | |
item = this.create(text) | |
} | |
return item | |
} | |
Storage.prototype.delete = function(id) { | |
if (isNaN(id)) return 'not valid' | |
if (!this.items[id]) return 'not found' | |
const item = this.items[id] | |
this.items = Object.assign({}, | |
Object.keys(this.items).reduce((result, key) => { | |
if (key !== id) { result[key] = this.items[key] } | |
return result | |
}, {})) | |
return item | |
} | |
const Todos = new Storage() | |
module.exports = Todos |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const chai = require('chai') | |
const chaiHttp = require('chai-http') | |
const Todos = require('./todos.storage') | |
const app = require('./index') | |
const should = chai.should() | |
chai.use(chaiHttp) | |
describe('Todos', function() { | |
it('should create a todo on post', function(done) { | |
const text = 'Learning nodejs' | |
chai.request(app) | |
.post('/todos') | |
.send({ text }) | |
.end(function(err, res) { | |
res.should.have.status(201) | |
res.should.be.json | |
res.body.id.should.be.a('number') | |
res.body.text.should.equal(text) | |
done() | |
}) | |
}) | |
it('should retrieve todos on get', function(done) { | |
chai.request(app) | |
.get('/todos') | |
.end(function(err, res) { | |
res.should.have.status(200) | |
res.should.be.json | |
res.body[0].id.should.be.a('number') | |
res.body[0].text.should.a('string') | |
done() | |
}) | |
}) | |
it('should update a todo on put', function(done) { | |
const text = 'Learning nodejs is fun' | |
chai.request(app) | |
.put('/todos/0') | |
.send({ 'id': 0, text }) | |
.end(function(err, res) { | |
res.should.have.status(200) | |
res.should.be.json | |
res.body.id.should.be.a('number') | |
res.body.text.should.equal(text) | |
done() | |
}) | |
}) | |
it('should delete a todo on delete', function(done) { | |
const text = 'Learning nodejs is fun' | |
chai.request(app) | |
.delete('/todos/0') | |
.send({ 'id': 0 }) | |
.end(function(err, res) { | |
res.should.have.status(200) | |
res.should.be.json | |
res.body.id.should.be.a('number') | |
res.body.text.should.equal(text) | |
done() | |
}) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment