Fix PUT should replace resource

This commit is contained in:
Typicode
2015-10-03 16:03:29 +02:00
parent 9f596a7a64
commit 2d278fbcb8
5 changed files with 21 additions and 16 deletions

View File

@ -21,7 +21,7 @@
"morgan": "^1.3.1", "morgan": "^1.3.1",
"node-uuid": "^1.4.2", "node-uuid": "^1.4.2",
"pluralize": "^1.1.2", "pluralize": "^1.1.2",
"underscore-db": "^0.9.0", "underscore-db": "^0.9.1",
"update-notifier": "^0.5.0", "update-notifier": "^0.5.0",
"yargs": "^3.10.0" "yargs": "^3.10.0"
}, },

View File

@ -215,8 +215,11 @@ module.exports = function (db, name) {
req.body[key] = utils.toNative(req.body[key]) req.body[key] = utils.toNative(req.body[key])
} }
var resource = db(name) var id = utils.toNative(req.params.id)
.updateById(utils.toNative(req.params.id), req.body)
var resource = req.method === 'PATCH' ?
db(name).updateById(id, req.body) :
db(name).replaceById(id, req.body)
if (resource) { if (resource) {
res.locals.data = resource res.locals.data = resource

View File

@ -1,5 +1,4 @@
var express = require('express') var express = require('express')
var utils = require('../utils')
module.exports = function (db, name) { module.exports = function (db, name) {
@ -11,18 +10,19 @@ module.exports = function (db, name) {
} }
function create (req, res, next) { function create (req, res, next) {
for (var prop in req.body) {
req.body[prop] = utils.toNative(req.body[prop])
}
res.locals.data = db.object[name] = req.body res.locals.data = db.object[name] = req.body
res.status(201) res.status(201)
next() next()
} }
function update (req, res, next) { function update (req, res, next) {
if (req.method === 'PUT') {
delete db.object[name]
db.object[name] = {}
}
for (var prop in req.body) { for (var prop in req.body) {
db.object[name][prop] = utils.toNative(req.body[prop]) db.object[name][prop] = req.body[prop]
} }
res.locals.data = db.object[name] res.locals.data = db.object[name]

View File

@ -408,17 +408,19 @@ describe('Server', function () {
}) })
describe('PUT /:resource/:id', function () { describe('PUT /:resource/:id', function () {
it('should respond with json and update resource', function (done) { it('should respond with json and replace resource', function (done) {
var post = {id: 1, booleanValue: true, integerValue: 1}
request(server) request(server)
.put('/posts/1') .put('/posts/1')
.send({id: 1, body: 'bar', booleanValue: 'true', integerValue: '1'}) // body property omitted to test that the resource is replaced
.send({id: 1, booleanValue: 'true', integerValue: '1'})
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect({id: 1, body: 'bar', booleanValue: true, integerValue: 1}) .expect(post)
.expect(200) .expect(200)
.end(function (err, res) { .end(function (err, res) {
if (err) return done(err) if (err) return done(err)
// assert it was created in database too // assert it was created in database too
assert.deepEqual(db.posts[0], {id: 1, body: 'bar', booleanValue: true, integerValue: 1}) assert.deepEqual(db.posts[0], post)
done() done()
}) })
}) })

View File

@ -44,18 +44,18 @@ describe('Server', function () {
}) })
describe('PUT /:resource', function () { describe('PUT /:resource', function () {
it('should uptade resource', function (done) { it('should update resource', function (done) {
var user = { name: 'bar' } var user = { name: 'bar' }
request(server) request(server)
.put('/user') .put('/user')
.send(user) .send(user)
.expect(db.user) .expect(user)
.expect(200, done) .expect(200, done)
}) })
}) })
describe('PATCH /:resource', function () { describe('PATCH /:resource', function () {
it('should uptade resource', function (done) { it('should update resource', function (done) {
request(server) request(server)
.patch('/user') .patch('/user')
.send({ name: 'bar' }) .send({ name: 'bar' })