Skip to content

Commit

Permalink
feature/fetch-one-center
Browse files Browse the repository at this point in the history
- implement controller to fetch one center
- implement controller to fetch events for a particular center
- write tests for fetch one center and its events
  • Loading branch information
Billmike committed Apr 18, 2018
1 parent ce90a86 commit 0091cbb
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
* Users can delete events they own
* Users can get all events in the application
* Users can get all the centers in the application
* Users can search tje application for centers using center name or location
* Users can search the application for centers using center name or location
* Users can fetch a single center to view all upcoming events

### Contributing
Receiving no contributions at this time.
18 changes: 18 additions & 0 deletions server/controllers/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,24 @@ class CenterController {
});
});
}
static getOneCenter(request, response) {
Center.findById(request.params.centerId)
.then((foundCenter) => {
if (!foundCenter) {
return response.status(404).json({
message: 'No center found.'
});
}
return response.status(200).json({
message: 'Center found',
centerDetail: foundCenter
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default CenterController;
21 changes: 21 additions & 0 deletions server/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ class Events {
});
});
}
static getCenterEvents(request, response) {
Event.findAll({
where: {
venue: request.params.venueId
}
}).then((centerEvents) => {
if (centerEvents.length === 0) {
return response.status(200).json({
message: 'No events yet for this center.'
});
}
return response.status(200).json({
message: `${centerEvents.length} upcoming events in this center`,
upcomingEvents: centerEvents
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Events;
2 changes: 2 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@ module.exports = (app) => {
sessionControl.isUser, eventController.deleteEvent
);
app.get('/api/v1/centers/search', centerController.searchCenters);
app.get('/api/v1/center/:centerId', centerController.getOneCenter);
app.get('/api/v1/center/events/:venueId', eventController.getCenterEvents);
};
55 changes: 55 additions & 0 deletions server/seeders/20180418211657-main-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

module.exports = {
up: (queryInterface, Sequelize) => {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
return queryInterface.bulkInsert('Events', [
{
name: 'Biker sports event',
duration: 2,
date: '2018-03-05 12:01:18.936+01',
organizer: 1,
venue: 1,
createdAt: '2018-03-05 12:01:18.936+01',
updatedAt: '2018-03-05 12:01:18.936+01'
},
{
name: 'NBA Playoffs',
duration: 2,
date: '2018-03-05 12:01:18.936+01',
organizer: 1,
venue: 1,
createdAt: '2018-03-05 12:01:18.936+01',
updatedAt: '2018-03-05 12:01:18.936+01'
},
{
name: 'Paralympics',
duration: 2,
date: '2018-03-05 12:01:18.936+01',
organizer: 1,
venue: 1,
createdAt: '2018-03-05 12:01:18.936+01',
updatedAt: '2018-03-05 12:01:18.936+01'
}
]);
},

down: (queryInterface, Sequelize) => {
/*
Add reverting commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkDelete('Person', null, {});
*/
}
};
22 changes: 21 additions & 1 deletion server/tests/center.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('Tests for Centers endpoint', () => {
}
);
});
describe('Get all centers endpoint', () => {
describe('Get centers endpoint', () => {
it('should fetch all centers in the application', (done) => {
request.get(centerApi)
.set('Connection', 'keep alive')
Expand All @@ -266,6 +266,26 @@ describe('Tests for Centers endpoint', () => {
done();
});
});
it('should return a 404 error if no center is found', (done) => {
request.get('/api/v1/center/1000')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(404);
done();
});
});
it('should fetch a single center in the application', (done) => {
request.get('/api/v1/center/1')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
done();
});
});
});
describe('Search centers endpoint test', () => {
it(
Expand Down
16 changes: 12 additions & 4 deletions server/tests/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ describe('Tests for Events API', () => {
done();
});
});
it('should fetch the events for a single center', (done) => {
request.get('/api/v1/center/events/1')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
done();
});
});
});
describe('Edit events test', () => {
it('should fail to edit an event if a user is not signed in', (done) => {
Expand Down Expand Up @@ -174,8 +184,7 @@ describe('Tests for Events API', () => {
}
);
it('should edit an event successfully', (done) => {
request.put(`/api/v1/event/${eventSeed
.id}?token=${secondDummyUser.token}`)
request.put(`/api/v1/event/4?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down Expand Up @@ -213,8 +222,7 @@ describe('Tests for Events API', () => {
});
});
it('should delete an event', (done) => {
request.delete(`/api/v1/event/${eventSeed
.id}?token=${secondDummyUser.token}`)
request.delete(`/api/v1/event/4?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down

0 comments on commit 0091cbb

Please sign in to comment.