Skip to content

Commit

Permalink
feat(countryFind): add find country route
Browse files Browse the repository at this point in the history
  • Loading branch information
Clm-Roig committed Sep 10, 2022
1 parent b6e059c commit a0ee4cc
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
30 changes: 30 additions & 0 deletions api/controllers/v1/country/find.js
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);
}
};
38 changes: 38 additions & 0 deletions assets/swaggerV1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tags:
- name: authentication
- name: caves
- name: cavers
- name: countries
- name: descriptions
- name: documents
- name: entrances
Expand Down Expand Up @@ -2783,6 +2784,31 @@ paths:
items:
$ref: '#/components/schemas/Language'

'/countries/{id}':
get:
tags:
- countries
description: Get a country by its id (iso 2).
parameters:
- name: id
in: path
description: Country id
example: FR, GB, IT
required: true
schema:
type: string
responses:
'200':
description: Successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/Country'
'404':
description: Resource not found

'/documents/check-rows':
post:
tags:
Expand Down Expand Up @@ -3030,6 +3056,18 @@ components:
count:
type: integer

Country:
type: object
properties:
id:
type: string
iso3:
type: string
nativeName:
type: string
numeric:
type: integer

Description:
type: object
properties:
Expand Down
1 change: 1 addition & 0 deletions config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ module.exports.policies = {
'v1/comment/update': 'tokenAuth',

// Country
'v1/country/find': true,
'v1/country/subscribe': ['tokenAuth', 'leaderAuth'],
'v1/country/unsubscribe': ['tokenAuth', 'leaderAuth'],

Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ module.exports.routes = {
'GET /api/comments/timeinfos/:entry': 'v1/comment/get-entrance-time-infos',

// Country
'GET /api/v1/countries/:id': 'v1/country/find',
'POST /api/v1/countries/:id/subscribe': 'v1/country/subscribe',
'POST /api/v1/countries/:id/unsubscribe': 'v1/country/unsubscribe',

Expand Down
31 changes: 31 additions & 0 deletions test/integration/4_routes/Countries/find.test.js
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();
});
});
});
});

0 comments on commit a0ee4cc

Please sign in to comment.