-
Notifications
You must be signed in to change notification settings - Fork 10
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
18 changed files
with
714 additions
and
4 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
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,79 @@ | ||
import dotenv from 'dotenv'; | ||
import cloudinary from 'cloudinary'; | ||
import Response from '../helpers/Response'; | ||
import db from '../database/models'; | ||
|
||
dotenv.config(); | ||
const { Accomodation, Room } = db; | ||
cloudinary.v2.config({ | ||
cloud_name: process.env.CLOUDINARY_NAME, | ||
api_key: process.env.CLOUDINARY_API, | ||
api_secret: process.env.CLOUDINARY_SECRET | ||
}); | ||
|
||
/** | ||
* Accomodation class | ||
*/ | ||
class AccomodationController { | ||
/** | ||
* @description - create accomodation center | ||
* @param {Object} req - HTTP Request object | ||
* @param {Object} res - HTTP Response object | ||
* @returns {object} objectu | ||
*/ | ||
static async createAccomodation(req, res) { | ||
const { | ||
name, branchId, capacity, address | ||
} = req.body; | ||
|
||
try { | ||
const image = req.file ? await cloudinary.uploader.upload(req.file.path) : null; | ||
const imgurl = image ? image.public_id : null; | ||
const { dataValues } = await Accomodation.create({ | ||
name, | ||
branchId, | ||
capacity, | ||
status: 'available', | ||
imgurl, | ||
address | ||
}); | ||
return res.status(200).json(new Response( | ||
true, | ||
200, | ||
'Successfully created acoomodation center', | ||
dataValues | ||
)); | ||
} catch (error) { | ||
return res.status(500).json( | ||
new Response(false, 500, error.message) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @description - Add rooms to accomodation center | ||
* @param {object} req - HTTP Request object | ||
* @param {object} res - HTTP Response object | ||
* @returns {object} -object | ||
*/ | ||
static async addRoom(req, res) { | ||
const { name, accomodationId, type } = req.body; | ||
|
||
try { | ||
const { dataValues } = await Room.create({ | ||
name, | ||
type, | ||
accomodationId | ||
}); | ||
return res.status(200).json( | ||
new Response(true, 200, 'Successfully added a room', dataValues) | ||
); | ||
} catch (error) { | ||
return res.status(500).json( | ||
new Response(false, 500, error.message) | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default AccomodationController; |
23 changes: 23 additions & 0 deletions
23
src/database/migrations/20190904154344-add-fields-accomodation.js
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,23 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.addColumn( | ||
'Accomodations', | ||
'imgurl', | ||
{ | ||
type: Sequelize.STRING | ||
} | ||
), | ||
queryInterface.addColumn( | ||
'Accomodations', | ||
'address', | ||
{ | ||
type: Sequelize.STRING | ||
} | ||
), | ||
]), | ||
|
||
down: queryInterface => Promise.all([ | ||
queryInterface.removeColumn('Accomodations', 'imgurl'), | ||
queryInterface.removeColumn('Accomodations', 'address'), | ||
]) | ||
}; |
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 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";').then(() => queryInterface.createTable('Rooms', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.DataTypes.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()'), | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
type: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
accomodationId: { | ||
type: Sequelize.DataTypes.UUID, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
references: { | ||
model: 'Accomodations', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.fn('now') | ||
} | ||
})), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Rooms') | ||
}; |
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,16 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Room = sequelize.define('Room', { | ||
name: DataTypes.STRING, | ||
type: DataTypes.STRING, | ||
accomodationId: DataTypes.UUID | ||
}, {}); | ||
Room.associate = models => { | ||
Room.belongsTo(models.Accomodation, { | ||
foreignKey: 'accomodationId', | ||
as: 'accomodation', | ||
onDelete: 'CASCADE', | ||
onUpdate: '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,29 @@ | ||
module.exports = { | ||
up: (queryInterface) => queryInterface.bulkInsert('Rooms', [ | ||
{ | ||
id: '3dd3b34a-1254-495e-a688-36afda199619', | ||
name: 'Room 1', | ||
type: 'Executive Room', | ||
accomodationId: '3dd3b34a-7554-425e-a688-36afda199619', | ||
}, | ||
{ | ||
id: '3dd3b34a-1254-495e-a688-36afda133619', | ||
name: 'Room 2', | ||
type: 'Single Room', | ||
accomodationId: '3dd3b34a-7554-425e-c688-36afda199619', | ||
}, | ||
{ | ||
id: '6dd3b34a-1254-495e-a628-36afda133919', | ||
name: 'Room 3', | ||
type: 'King Room', | ||
accomodationId: '3dd3b34a-7554-425e-c688-36afda199619', | ||
}, | ||
{ | ||
id: '3dd3b34a-4454-495e-a688-36afda903619', | ||
name: 'Room 4', | ||
type: 'Single Room', | ||
accomodationId: '3dd3b34a-7554-425e-a688-36afda199619', | ||
} | ||
]), | ||
down: queryInterface => queryInterface.bulkDelete('Rooms', null, {}) | ||
}; |
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 @@ | ||
import { Router } from 'express'; | ||
import Token from '../helpers/Token'; | ||
import AccomodationController from '../controllers/Accomodation'; | ||
import validator from '../middlewares/validator'; | ||
import { createAccomodation, addRoom } from '../validation/accomodationSchema'; | ||
import upload from '../utils/upload'; | ||
|
||
const accomodationRoutes = Router(); | ||
|
||
accomodationRoutes.post('/create', | ||
Token.verifyAdminToken('travel admin'), upload.single('imgurl'), validator(createAccomodation), AccomodationController.createAccomodation); | ||
|
||
accomodationRoutes.post('/addroom', | ||
Token.verifyAdminToken('travel admin'), validator(addRoom), AccomodationController.addRoom); | ||
|
||
|
||
export default accomodationRoutes; |
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,26 @@ | ||
import multer from 'multer'; | ||
|
||
const storage = multer.diskStorage({ | ||
filename: (req, file, cb) => { | ||
cb(null, Date.now() + file.originalname); | ||
} | ||
}); | ||
|
||
const fileFilter = (req, file, cb) => { | ||
if ( | ||
file.mimetype === 'image/jpg' | ||
|| file.mimetype === 'image/png' | ||
|| file.mimetype === 'image/jpeg' | ||
) { | ||
cb(null, true); | ||
} | ||
cb(null, false); | ||
}; | ||
|
||
const upload = multer({ | ||
storage, | ||
fileFilter, | ||
limits: { fileSize: 500000 }, | ||
}); | ||
|
||
export default upload; |
Oops, something went wrong.