Skip to content

Commit

Permalink
feature/implement-search
Browse files Browse the repository at this point in the history
write controller for search function
- write tests for search function
  • Loading branch information
Billmike committed Apr 18, 2018
1 parent 5f6dd2a commit 0951c21
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* Users can modify events they own
* 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

### Contributing
Receiving no contributions at this time.
31 changes: 31 additions & 0 deletions server/controllers/center.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Sequelize from 'sequelize';
import db from '../models';
import serverError from '../errorHandler/serverError';
import validateInput from '../validators/validateAddCenter';

const { Center } = db;
const Op = Sequelize.Op;

class CenterController {
static createCenter(request, response) {
Expand Down Expand Up @@ -125,6 +127,35 @@ class CenterController {
});
});
}
static searchCenters(request, response) {
const { search } = request.query;
return Center.findAll({
where: {
[Op.or]: {
name: {
[Op.iLike]: `%${search}`
},
location: {
[Op.iLike]: `%${search}`
}
}
}
}).then((foundCenters) => {
if (foundCenters.length == 0) {
return response.status(200).json({
message: 'No center found with this name or location'
});
}
return response.status(200).json({
message: `Found ${foundCenters.length} center(s) matching ${search}.`,
centerDetails: foundCenters
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default CenterController;
1 change: 1 addition & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ module.exports = (app) => {
'/api/v1/event/:eventId', sessionControl.isLoggedIn,
sessionControl.isUser, eventController.deleteEvent
);
app.get('/api/v1/centers/search', centerController.searchCenters);
};
31 changes: 30 additions & 1 deletion server/tests/center.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,34 @@ describe('Tests for Centers endpoint', () => {
done();
});
});
})
});
describe('Search centers endpoint test', () => {
it(
'should return an empty array if no center is found for search',
(done) => {
request.get('/api/v1/centers/search?search=James palace')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.status).to.equal(200);
expect(response.body.message).to
.equal('No center found with this name or location');
done();
});
}
);
it('should handle a server error when one occurs', (done) => {
request.get('/api/v1/centers/search?search=Yaba Beach')
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.end((error, response) => {
expect(response.body.message).to.equal('Found 1 center(s) matching Yaba Beach.');
expect(response.body.centerDetails).to.be.an('array');
expect(response.status).to.equal(200);
done();
});
});
});
});
19 changes: 18 additions & 1 deletion server/tests/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,25 @@ describe('Tests for Events API', () => {
done();
});
});
it(
'should fail to modify an event if the user did not create it',
(done) => {
request.put(`/api/v1/event/${eventSeed.id}?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(eventSeed)
.end((error, response) => {
expect(response.status).to.equal(401);
expect(response.body.message)
.to.equal('You do not have the privilege to modify this resource');
done();
});
}
);
it('should edit an event successfully', (done) => {
request.put(`/api/v1/event/${eventSeed.id}?token=${secondDummyUser.token}`)
request.put(`/api/v1/event/${eventSeed
.id}?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down

0 comments on commit 0951c21

Please sign in to comment.