Skip to content

Commit

Permalink
feature/implement-delete
Browse files Browse the repository at this point in the history
- implement delete function
- write tests for delete function
  • Loading branch information
Billmike committed Apr 17, 2018
1 parent 208c4b1 commit 5f6dd2a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
[![Build Status](https://travis-ci.org/Billmike/event-center-api.svg?branch=master)](https://travis-ci.org/Billmike/event-center-api) [![Coverage Status](https://coveralls.io/repos/github/Billmike/event-center-api/badge.svg?branch=feature%2Fimplement-modify-center)](https://coveralls.io/github/Billmike/event-center-api?branch=feature%2Fimplement-modify-center)
[![Build Status](https://travis-ci.org/Billmike/event-center-api.svg?branch=master)](https://travis-ci.org/Billmike/event-center-api) [![Coverage Status](https://coveralls.io/repos/github/Billmike/event-center-api/badge.svg?branch=master)](https://coveralls.io/github/Billmike/event-center-api?branch=master)

### Simple CRUD API that manages your centers and events

### API Features
* Admin can create centers
* Admin can edit center details
* Users can sign up and sign-in
* Users can create events
* Users can modify events they own
* Users can delete events they own
* Users can get all events in the application

### Contributing
Receiving no contributions at this time.
25 changes: 25 additions & 0 deletions server/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,31 @@ class Events {
});
});
}
static deleteEvent(request, response) {
Event.findById(request.params.eventId)
.then((event) => {
if (!event) {
return response.status(404).json({
message: 'No event found.'
});
}
if (event.organizer != request.userDetails.id) {
return response.status(401).json({
message: 'You do not have the privilege to modify this resource'
});
}
return event.destroy().then(() => {
response.status(200).json({
message: 'Event successfully deleted.',
eventDetails: event
});
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Events;
4 changes: 4 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ module.exports = (app) => {
sessionControl.isLoggedIn, sessionControl.isUser,
eventController.modifyEvent
);
app.delete(
'/api/v1/event/:eventId', sessionControl.isLoggedIn,
sessionControl.isUser, eventController.deleteEvent
);
};
36 changes: 36 additions & 0 deletions server/tests/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,40 @@ describe('Tests for Events API', () => {
});
});
});
describe('Delete event test', () => {
it('should throw an error if user is not logged in', (done) => {
request.delete('/api/v1/event/eventSeed.id')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(401);
done();
});
});
it('should throw an error is no event is found', (done) => {
request.delete(`/api/v1/event/100?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(404);
expect(response.body.message).to.equal('No event found.');
done();
});
});
it('should delete an event', (done) => {
request.delete(`/api/v1/event/${eventSeed
.id}?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
expect(response.body).to.be.an('object');
expect(response.body.eventDetails).to.be.an('object');
done();
});
});
});
});

0 comments on commit 5f6dd2a

Please sign in to comment.