Skip to content

Commit

Permalink
Adds update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DeadlockDruid committed Sep 24, 2018
1 parent cffbe56 commit 240c652
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"body-parser": "^1.18.3",
"expect": "^23.6.0",
"express": "^4.16.3",
"lodash": "^4.17.11",
"mocha": "^5.2.0",
"mongodb": "^3.1.6",
"mongoose": "^5.2.17",
Expand Down
41 changes: 33 additions & 8 deletions server/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
let express = require('express');
let bodyParser = require('body-parser');
let { ObjectID } = require('mongodb');
const _ = require('lodash');
const express = require('express');
const bodyParser = require('body-parser');
const { ObjectID } = require('mongodb');

let { mongoose } = require('./db/mongoose');
let { Todo } = require('./models/todo');
let { User } = require('./models/user');
const { mongoose } = require('./db/mongoose');
const { Todo } = require('./models/todo');
const { User } = require('./models/user');

let app = express();
let port = process.env.PORT || 3000;
const app = express();
const port = process.env.PORT || 3000;

app.use(bodyParser.json());

Expand Down Expand Up @@ -61,6 +62,30 @@ app.delete('/todos/:id', (req, res) => {
}).catch((e) => res.status(400).send());
});

app.put('/todos/:id', (req, res) => {
const id = req.params.id;
const body = _.pick(req.body, ['text', 'completed']);

if (!ObjectID.isValid(id)) {
return res.status(404).send();
}

if (_.isBoolean(body.completed) && body.completed) {
body.completedAt = new Date().getTime();
} else {
body.completed = false;
body.completedAt = null;
}

Todo.findByIdAndUpdate(id, {$set: body}, {new: true}).then((todo) => {
if (!todo) {
return res.status(404).send();
}

res.send(todo);
}).catch((e) => res.status(400).send());
});

app.listen(port, () => {
console.log(`Started on port: ${port}`);
});
Expand Down

0 comments on commit 240c652

Please sign in to comment.