-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(countryFind): add find country route
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const ControllerService = require('../../../services/ControllerService'); | ||
const ErrorService = require('../../../services/ErrorService'); | ||
|
||
const { checkIfExists } = sails.helpers; | ||
|
||
module.exports = async (req, res) => { | ||
const countryId = req.param('id'); | ||
if (!(await checkIfExists('id', countryId, TCountry))) { | ||
return res.notFound({ | ||
message: `Country with id ${countryId} not found.`, | ||
}); | ||
} | ||
|
||
try { | ||
const country = await TCountry.findOne(countryId); | ||
const params = { | ||
controllerMethod: 'Country.find', | ||
searchedItem: `Country of id ${countryId}`, | ||
}; | ||
// TODO: if needed elsewhere, refactor this in the MappingService, using a ConvertToCountryModel function | ||
const formattedCountry = { | ||
...country, | ||
latitude: parseFloat(country.latitude), | ||
longitude: parseFloat(country.longitude), | ||
}; | ||
return ControllerService.treat(req, null, formattedCountry, params, res); | ||
} catch (e) { | ||
return ErrorService.getDefaultErrorHandler(res)(e); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const supertest = require('supertest'); | ||
const should = require('should'); | ||
|
||
const COUNTRY_PROPERTIES = ['id', 'nativeName']; | ||
|
||
describe('Country features', () => { | ||
describe('Find', () => { | ||
it('should return code 404', (done) => { | ||
supertest(sails.hooks.http.app) | ||
.get('/api/v1/countries/abcdefgh') | ||
.set('Content-type', 'application/json') | ||
.set('Accept', 'application/json') | ||
.expect(404, done); | ||
}); | ||
it('should return code 200', (done) => { | ||
supertest(sails.hooks.http.app) | ||
.get('/api/v1/countries/FR') | ||
.set('Content-type', 'application/json') | ||
.set('Accept', 'application/json') | ||
.expect(200) | ||
.end((err, res) => { | ||
if (err) return done(err); | ||
const { body: organization } = res; | ||
should(organization).have.properties(COUNTRY_PROPERTIES); | ||
should(organization.id).not.be.empty(); | ||
should(organization.nativeName).not.be.empty(); | ||
return done(); | ||
}); | ||
}); | ||
}); | ||
}); |