Skip to content

Commit

Permalink
update PUT /todos/:id to use sequelize
Browse files Browse the repository at this point in the history
  • Loading branch information
KaaPex committed Jun 14, 2016
1 parent ace47df commit 634783f
Showing 1 changed file with 18 additions and 19 deletions.
37 changes: 18 additions & 19 deletions server.js
Expand Up @@ -108,32 +108,31 @@ app.put('/todos/:id', (req, res) => {
"error": "id is in incorrect format"
});
} else {
var matchesTodo = _.findWhere(todos, {
id: todoId
});
if (!matchesTodo) {
res.status(404).json({
"error": "no todo found with that id"
});
}

var body = _.pick(req.body, 'description', 'completed');
var validAttributes = {};
var attributes = {};

if (body.hasOwnProperty('completed') && _.isBoolean(body.completed)) {
validAttributes.completed = body.completed;
} else if (body.hasOwnProperty('completed')) {
return res.status(400).send();
if (body.hasOwnProperty('completed')) {
attributes.completed = body.completed;
}

if (body.hasOwnProperty('description') && _.isString(body.description) && body.description.trim().length !== 0) {
validAttributes.description = body.description;
} else if (body.hasOwnProperty('description')) {
return res.status(400).send();
if (body.hasOwnProperty('description')) {
attributes.description = body.description;
}

_.extend(matchesTodo, validAttributes);
res.json(matchesTodo);
db.todo.findById(todoId).then( (todo) => {
if (todo) {
todo.update(attributes).then( (todo) => {
res.json(todo);
}, (e) => {
res.status(400).json(e);
});
} else {
res.status(404).send();
}
}, () => {
res.status(500).send();
});
}
});

Expand Down

0 comments on commit 634783f

Please sign in to comment.