-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ft-167727288] Most Traveled Destinations
- Loading branch information
1 parent
c9771f9
commit 1363df0
Showing
14 changed files
with
362 additions
and
61 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,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
25
src/database/migrations/20190910142213-create-destination.js
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
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,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' | ||
} | ||
} | ||
} | ||
} | ||
}; |
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,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' | ||
} | ||
} | ||
} | ||
} | ||
}; |
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,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; | ||
}; |
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
File renamed without changes.
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,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; |
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,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 | ||
}; | ||
} | ||
}; |
Oops, something went wrong.