Skip to content

Commit

Permalink
[Feature #159068502] Add /PUT/:id entries endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
olusoladavid committed Jul 20, 2018
1 parent 038fc80 commit cfcce8f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 4 deletions.
2 changes: 1 addition & 1 deletion server/controllers/addEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const addEntry = (req, res) => {
const entry = req.body;
entry.id = entries.length + 1;
entries.push(entry);
return res.status(201).json(entries);
return res.status(201).json(entry);
};

export default addEntry;
19 changes: 19 additions & 0 deletions server/controllers/modifyEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { validationResult } from 'express-validator/check';
import entries from '../db/entries';

const modifyEntry = (req, res) => {
const error = validationResult(req);
const indexOfFound = entries.findIndex(entry => entry.id === Number(req.params.id));

if (indexOfFound > -1 && error.isEmpty()) {
const found = { ...entries[indexOfFound] };
entries[indexOfFound] = { ...found, ...req.body };
return res.status(200).json(entries[indexOfFound]);
}
if (indexOfFound === -1) {
return res.status(404).json({ errors: [{ msg: 'Entry does not exist' }] });
}
return res.status(400).json({ errors: error.array() });
};

export default modifyEntry;
2 changes: 2 additions & 0 deletions server/db/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const entries = [
content: `After many years of pushing for a change, we finally broke through.
This time however I was spearheading my department. We finally got around to
start using version control.`,
isFavorite: true,
},
{
id: 2,
Expand All @@ -14,6 +15,7 @@ const entries = [
content: `It happened on a dusty evening. We were strolling down the park as I
accompanied her home. At the front of her basement, she looked deeply into my eyes
and came close. The message was clear. Now it's a memory.`,
isFavorite: false,
},
];

Expand Down
29 changes: 28 additions & 1 deletion server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { check } from 'express-validator/check';
import getAllEntries from '../controllers/getAllEntries';
import getEntry from '../controllers/getEntry';
import addEntry from '../controllers/addEntry';
import modifyEntry from '../controllers/modifyEntry';

const router = express.Router();

Expand All @@ -20,8 +21,34 @@ router.get('/entries/:id', getEntry);
/* POST a new entry */
router.post(
'/entries',
[check('timestamp').isInt(), check('title').isString(), check('content').isString()],
[
check('timestamp').isInt(),
check('title').isString(),
check('content').isString(),
check('isFavorite').isBoolean(),
],
addEntry,
);

/* PUT new data in existing entry */
router.put(
'/entries/:id',
[
check('timestamp')
.not()
.exists()
.withMessage('Timestamp cannot be modified'),
check('title')
.isString()
.optional(),
check('content')
.isString()
.optional(),
check('isFavorite')
.isBoolean()
.optional(),
],
modifyEntry,
);

export default router;
60 changes: 58 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('/GET/:id entries', () => {
expect(res.body).to.have.property('timestamp');
expect(res.body).to.have.property('title');
expect(res.body).to.have.property('content');
expect(res.body).to.have.property('isFavorite');
done();
});
});
Expand All @@ -68,15 +69,20 @@ describe('/POST entries', () => {
timestamp: 153677782990,
title: 'title',
content: 'content',
isFavorite: false,
};
chai
.request(app)
.post('/api/v1/entries')
.send(sampleEntry)
.end((err, res) => {
expect(res).to.have.status(201);
expect(res.body).to.be.an('array');
expect(res.body.length).to.be.eql(entriesLengthBeforeRequest + 1);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('timestamp', sampleEntry.timestamp);
expect(res.body).to.have.property('title', sampleEntry.title);
expect(res.body).to.have.property('content', sampleEntry.content);
expect(res.body).to.have.property('isFavorite', sampleEntry.isFavorite);
expect(entries.length).to.be.eql(entriesLengthBeforeRequest + 1);
done();
});
});
Expand All @@ -96,3 +102,53 @@ describe('/POST entries', () => {
});
});
});

describe('/PUT/:id entries', () => {
it('should modify a previously created entry', (done) => {
const entriesLengthBeforeRequest = entries.length;
const modification = {
title: 'another title',
isFavorite: true,
};
chai
.request(app)
.put('/api/v1/entries/1')
.send(modification)
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body).to.be.an('object');
expect(res.body.id).to.be.eql(1);
expect(res.body).to.have.property('title', modification.title);
expect(res.body).to.have.property('isFavorite', modification.isFavorite);
expect(entries.length).to.be.eql(entriesLengthBeforeRequest);
done();
});
});

it('should reject invalid modification', (done) => {
const entriesLengthBeforeRequest = entries.length;
chai
.request(app)
.put('/api/v1/entries/1')
.send({ title: 5 })
.end((err, res) => {
expect(res).to.have.status(400);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('errors');
expect(entries.length).to.be.eql(entriesLengthBeforeRequest);
done();
});
});

it('should not modify an entry', (done) => {
chai
.request(app)
.put('/api/v1/entries/0')
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body).to.be.an('object');
expect(res.body).to.have.property('errors');
done();
});
});
});

0 comments on commit cfcce8f

Please sign in to comment.