Skip to content

Commit

Permalink
[Feature #159068545] Add /DELETE/:id entries endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
olusoladavid committed Jul 20, 2018
1 parent cfcce8f commit bebc0c4
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
66 changes: 65 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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/<entryId>` | Fetch a single entry |
| POST `/entries` | Create an entry |
| PUT `/entries/<entryId>` | Modify an entry |
| DELETE `/entries/<entryId>` | 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions server/controllers/deleteEntry.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -51,4 +52,7 @@ router.put(
modifyEntry,
);

/* DELETE a single entry */
router.delete('/entries/:id', deleteEntry);

export default router;
29 changes: 29 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit bebc0c4

Please sign in to comment.