diff --git a/README.md b/README.md index 613ea84..dbc2301 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,71 @@ [![Build Status](https://travis-ci.org/olusoladavid/my-diary.svg?branch=ft-rest-api-endpoints-159069640)](https://travis-ci.org/olusoladavid/my-diary) -[![Coverage Status](https://coveralls.io/repos/github/olusoladavid/my-diary/badge.svg?branch=ft-rest-api-endpoints-159069640)](https://coveralls.io/github/olusoladavid/my-diary?branch=ft-rest-api-endpoints-159069640) +[![Coverage Status](https://coveralls.io/repos/github/olusoladavid/my-diary/badge.svg?branch=develop)](https://coveralls.io/github/olusoladavid/my-diary?branch=ft-rest-api-endpoints-159069640) [![Maintainability](https://api.codeclimate.com/v1/badges/28ee352fbbd498a8cafd/maintainability)](https://codeclimate.com/github/olusoladavid/my-diary/maintainability) # my-diary MyDiary is an online journal where users can pen down their thoughts and feelings + +## Required Features + +- Users can create an account and log in. +- User can view all entries to their diary. +- Users can view the contents of a diary entry. +- Users can add or modify an entry. +- Users can delete an entry + +## Additional Features + +- Users can set and get daily notifications that prompt them to add an entry to their diary + +## Technologies + +- Nodejs +- Express +- Mocha (with Chai as assertion library) +- Babel (ES6+ to ES5 transpilation) +- ESLint (with Airbnb style guide) + +## API Endpoints + +| Endpoint | Functionality | +| --------------------------- | -------------------- | +| GET `/entries` | Fetch all entries | +| GET `/entries/` | Fetch a single entry | +| POST `/entries` | Create an entry | +| PUT `/entries/` | Modify an entry | +| DELETE `/entries/` | Delete an entry | + +## Build Setup + +``` +clone repo and cd into directory + +git clone https://github.com/olusoladavid/my-diary.git +``` + +``` +# install dependencies +npm install + +#serve in development environment +npm run dev + +# build for production +npm run build +``` + +## Testing + +``` +# Run test cases +npm test +``` + +API Endpoint: https://my-diary-api.herokuapp.com/api/v1 + +UI Template: https://olusoladavid.github.io/my-diary/UI/ + +## License + +- MIT diff --git a/package.json b/package.json index 20f7c8a..02563fa 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "MyDiary is an online journal where users can pen down their thoughts and feelings", "main": "index.js", "engines": { - "node": "8.9.4" + "node": "8.11.1" }, "scripts": { "dev": "nodemon server/index.js --exec babel-node", diff --git a/server/controllers/deleteEntry.js b/server/controllers/deleteEntry.js new file mode 100644 index 0000000..101fbd0 --- /dev/null +++ b/server/controllers/deleteEntry.js @@ -0,0 +1,13 @@ +import entries from '../db/entries'; + +const deleteEntry = (req, res) => { + const indexOfFound = entries.findIndex(entry => entry.id === Number(req.params.id)); + if (indexOfFound > -1) { + entries.splice(indexOfFound, 1); + res.status(204).json(); + } else { + res.status(404).json({ errors: [{ msg: 'Entry does not exist' }] }); + } +}; + +export default deleteEntry; diff --git a/server/routes/index.js b/server/routes/index.js index fc3f5a3..9768e79 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -4,6 +4,7 @@ import getAllEntries from '../controllers/getAllEntries'; import getEntry from '../controllers/getEntry'; import addEntry from '../controllers/addEntry'; import modifyEntry from '../controllers/modifyEntry'; +import deleteEntry from '../controllers/deleteEntry'; const router = express.Router(); @@ -51,4 +52,7 @@ router.put( modifyEntry, ); +/* DELETE a single entry */ +router.delete('/entries/:id', deleteEntry); + export default router; diff --git a/test/test.js b/test/test.js index df1cad9..653be4a 100644 --- a/test/test.js +++ b/test/test.js @@ -152,3 +152,32 @@ describe('/PUT/:id entries', () => { }); }); }); + +describe('/DELETE/:id entries', () => { + it('should delete a single entry by id', (done) => { + const entriesLengthBeforeRequest = entries.length; + chai + .request(app) + .delete('/api/v1/entries/1') + .end((err, res) => { + expect(res).to.have.status(204); + expect(res.body).to.be.eql({}); + expect(entries.length).to.be.eql(entriesLengthBeforeRequest - 1); + done(); + }); + }); + + it('should not return an entry', (done) => { + const entriesLengthBeforeRequest = entries.length; + chai + .request(app) + .delete('/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'); + expect(entries.length).to.be.eql(entriesLengthBeforeRequest); + done(); + }); + }); +});