Skip to content

Commit

Permalink
[ft-167727288] Most Traveled Destinations
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveFunso committed Sep 15, 2019
1 parent c9771f9 commit 1363df0
Show file tree
Hide file tree
Showing 14 changed files with 362 additions and 61 deletions.
13 changes: 13 additions & 0 deletions src/controllers/destinationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mostTraveledDestination } from '../services/destinationServices';
import { respondWithSuccess, respondWithWarning } from '../helpers/responseHandler';
import statusCode from '../helpers/statusCode';


export const destinationController = async (req, res) => {
try {
const destination = await mostTraveledDestination();
return respondWithSuccess(res, statusCode.success, 'resource successfully created', { maxDestination: destination });
} catch (error) {
return respondWithWarning(res, statusCode.internalServerError, 'Server Error');
}
};
25 changes: 0 additions & 25 deletions src/database/migrations/20190910142213-create-destination.js

This file was deleted.

24 changes: 0 additions & 24 deletions src/database/seeders/20190910152014-destination-seeder.js

This file was deleted.

140 changes: 138 additions & 2 deletions src/database/seeders/20190910225149-create-destination-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,145 @@ export default {
destination: 'Paris',
},
{
destination: 'Madrid',
destination: 'Saudi Arabia',
},
{
destination: 'United Arab Emirates',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
},
{
destination: 'Lagos',
},
{
destination: 'Kampala',
},
{
destination: 'Cairo',
},
{
destination: 'Texas',
},
{
destination: 'Paris',
},
{
destination: 'Saudi Arabia',
},
{
destination: 'United Arab Emirates',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
}, {
destination: 'Lagos',
},
{
destination: 'Kampala',
},
{
destination: 'Cairo',
},
{
destination: 'Texas',
},
{
destination: 'Paris',
},
{
destination: 'Saudi Arabia',
},
{
destination: 'United Arab Emirates',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
}, {
destination: 'Lagos',
},
{
destination: 'Kampala',
},
{
destination: 'Cairo',
},
{
destination: 'Texas',
},
{
destination: 'Paris',
},
{
destination: 'Saudi Arabia',
},
{
destination: 'United Arab Emirates',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Singapore',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
},
{
destination: 'Russia',
}
], {}),

down: queryInterface => queryInterface.bulkDelete('Destinations', null, {})
};
};
22 changes: 22 additions & 0 deletions src/docs/swagger/definitions/destination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const getFrequentDestination = {
type: 'object',
properties: {
success: {
type: 'boolean',
example: true
},
message: {
type: 'string',
example: 'resource successfully created'
},
payload: {
type: 'object',
properties: {
maxDestination: {
type: 'string',
example: 'Singapore'
}
}
}
}
};
38 changes: 38 additions & 0 deletions src/docs/swagger/paths/destination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export const getFrequentDestinationPath = {
get: {
tags: ['destination'],
security: [
{
BearerToken: []
}
],
summary: 'Get most traveled destination',
description: 'Users can get the most frequent destination',
responses: {
200: {
description: 'Most traveled destination successfully fetched',
schema: {
$ref: '#/definitions/getFrequentDestination'
}
},
401: {
description: 'Incorrect login details',
schema: {
$ref: '#/definitions/notAuthorized'
}
},
403: {
description: 'Forbidden access',
schema: {
$ref: '#/definitions/accessForbidden'
}
},
500: {
description: 'Server error',
schema: {
$ref: '#/definitions/serverError'
}
}
}
}
};
17 changes: 17 additions & 0 deletions src/helpers/frequentDestination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const maxDestination = myArr => {
const destStat = {};
const mostVisited = [];
myArr.forEach(destination => {
// eslint-disable-next-line no-unused-expressions
destStat[destination] > 0 ? destStat[destination] += 1 : destStat[destination] = 1;
});
const maxValue = Math.max(...Object.values(destStat));

Object.keys(destStat).forEach(key => {
if (destStat[key] === maxValue) {
mostVisited.push(key);
}
});

return mostVisited;
};
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { cloudinaryConfig } from './config/cloudinaryConfig';
import './config/passport';
import { socketConnection } from './helpers/socketIO';


const app = express();
const port = PORT || 3000;
app.use(bodyParser.json());
Expand Down Expand Up @@ -44,4 +45,5 @@ server.listen(port, () => {
console.log(`Server listening on port ${port}`);
});


export default app;
File renamed without changes.
10 changes: 10 additions & 0 deletions src/routes/api/destination.routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Router } from 'express';
import { destinationController } from '../../controllers/destinationController';
import { authenticateUserToken } from '../../middlewares/authentication';


const router = Router();

router.get('/', authenticateUserToken, destinationController);

export default router;
14 changes: 4 additions & 10 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import comments from './api/comment.routes';
import chats from './api/chats.routes';
import flight from './api/flight.routes';
import ratings from './api/rating.routes';
import destinations from './api/destination.routes';


const apiRouter = Router();

Expand All @@ -26,15 +28,7 @@ apiRouter.use('/api/v1/accommodations', ratings);
apiRouter.use('/api/v1/bookings', booking);
apiRouter.use('/api/v1', chats);
apiRouter.use('/api/v1/flights', flight);

export default apiRouter;








apiRouter.use('/api/v1/frequent-destination', destinations);


export default apiRouter;
29 changes: 29 additions & 0 deletions src/services/destinationServices.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

import Model from '../models';
import { respondWithWarning, statusCode } from '../helpers/responseHandler';
import { maxDestination } from '../helpers/frequentDestination';


const { Destination } = Model;


export const mostTraveledDestination = async (req, res) => {
try {
const rows = await Destination.findAll({
attributes: ['destination']
});
if (rows.length < 1) {
return respondWithWarning(
res,
statusCode.noResourceFound,
'Destination not found'
);
}
const destinations = rows.map(row => row.destination);
return maxDestination(destinations);
} catch (error) {
return {
errors: error
};
}
};
Loading

0 comments on commit 1363df0

Please sign in to comment.