-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,355 additions
and
13 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,69 @@ | ||
import models from '../db/models'; | ||
import Response from '../utils/response.utils'; | ||
|
||
const { Facility, Room } = models; | ||
|
||
/** | ||
* This class creates the facilities controllers | ||
*/ | ||
export default class FacilitiesController { | ||
/** | ||
* @param {object} req The facility details | ||
* @param {object} res The facility details returned after listing a facility | ||
* @returns {object} A newly created facility | ||
*/ | ||
static async createFacilities(req, res) { | ||
const { facilitiesValues } = req.body; | ||
try { | ||
const facility = await Facility.create( | ||
facilitiesValues, | ||
{ returning: true } | ||
); | ||
return Response.Success(res, facility, 201); | ||
} catch (error) { | ||
console.log(error) | ||
return Response.InternalServerError(res, 'Could not register facility'); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req The room details | ||
* @param {object} res The room details returned after creating a room | ||
* @returns {object} A newly registered room | ||
*/ | ||
static async createRoom(req, res) { | ||
const { roomsValues } = req.body; | ||
try { | ||
const room = await Room.create( | ||
roomsValues, | ||
{ returning: true } | ||
); | ||
|
||
return Response.Success(res, room, 201); | ||
} catch (error) { | ||
return Response.InternalServerError(res, 'Could not register rooms'); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res The facilities being returned | ||
* @returns {object} All the facilities on the facilities table | ||
*/ | ||
static async getAllFacilities(req, res) { | ||
try { | ||
const facilities = await Facility.findAll({ returning: true }); | ||
|
||
if (facilities.lenght < 1) { | ||
return res.status(404).json({ | ||
status: 'success', | ||
error: 'There is no facilities listed on barefoot nomad at this time' | ||
}); | ||
} | ||
|
||
return Response.Success(res, facilities, 200); | ||
} catch (error) { | ||
return Response.InternalServerError(res, 'Could not return facilities'); | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,5 +15,6 @@ module.exports = (sequelize) => { | |
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
|
||
return Like; | ||
}; |
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 |
---|---|---|
|
@@ -42,5 +42,6 @@ module.exports = (sequelize, DataTypes) => { | |
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
|
||
return Request; | ||
}; |
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 |
---|---|---|
|
@@ -25,5 +25,6 @@ module.exports = (sequelize, DataTypes) => { | |
onDelete: 'CASCADE' | ||
}) | ||
); | ||
|
||
return Room; | ||
}; |
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,47 @@ | ||
import FacilityUtils from '../utils/facility.utils'; | ||
import models from '../db/models'; | ||
|
||
const { Facility } = models; | ||
const { getFacilitiesDetails } = FacilityUtils; | ||
const { getRoomsDetails } = FacilityUtils; | ||
|
||
const FacilitiesChecks = { | ||
async getFacilitiesValues(req, res, next) { | ||
const facilitiesValues = getFacilitiesDetails(req.body); | ||
const isEmpty = Object.keys(facilitiesValues).length === 0; | ||
|
||
if (!isEmpty) { | ||
// make facilitiesValues available to the next middleware | ||
req.body.facilitiesValues = facilitiesValues; | ||
if (typeof (facilitiesValues.images) === 'string') { | ||
const { images } = facilitiesValues; | ||
req.body.facilitiesValues = { ...facilitiesValues, images: images.split(',') }; | ||
req.body.images = images.split(','); | ||
} | ||
return next(); | ||
} | ||
res.status(422).json({ | ||
status: 'error', | ||
error: 'You have not entered any facilities details' | ||
}); | ||
}, | ||
|
||
async getRoomsValues(req, res, next) { | ||
const roomsValues = getRoomsDetails(req.body); | ||
// ensures the user supplies, at least, one value to update | ||
const isEmpty = Object.keys(roomsValues).length === 0; | ||
|
||
if (!isEmpty) { | ||
// make userValues available to the next middleware | ||
req.body.roomsValues = roomsValues; | ||
return next(); | ||
} | ||
// Return a 422 error if user does not supply values to update. | ||
res.status(422).json({ | ||
status: 'error', | ||
error: 'You have not entered any room details' | ||
}); | ||
} | ||
}; | ||
|
||
export default FacilitiesChecks; |
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,28 @@ | ||
import express from 'express'; | ||
import auth from '../../middlewares/auth'; | ||
import facilitiesController from '../../controllers/facilities.controller'; | ||
import validateFacilities from '../../validation/facilities/facilities.validation'; | ||
import validateRooms from '../../validation/facilities/rooms.validation'; | ||
import FacilitiesChecks from '../../middlewares/facilities.middleware'; | ||
|
||
const router = express.Router(); | ||
|
||
/* Requests Routes Here */ | ||
router.post('/facilities', | ||
auth.verifyUserToken, | ||
auth.verifyTravelAdmin, | ||
FacilitiesChecks.getFacilitiesValues, | ||
validateFacilities, | ||
facilitiesController.createFacilities); | ||
|
||
router.post('/facilities/rooms', | ||
auth.verifyUserToken, | ||
auth.verifyTravelAdmin, | ||
FacilitiesChecks.getRoomsValues, | ||
validateRooms, | ||
facilitiesController.createRoom); | ||
|
||
router.get('/facilities', | ||
facilitiesController.getAllFacilities); | ||
|
||
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
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
Oops, something went wrong.