Skip to content

Commit

Permalink
feature(accommodation): create feature for users to comment/give feed…
Browse files Browse the repository at this point in the history
…back on accommodation facilities
  • Loading branch information
gmemmy committed Sep 17, 2019
1 parent 259e592 commit 6a6b62c
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/controllers/AccommodationController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import models from '../models';
import { HelperMethods } from '../utils';

const { Sequelize, Like, Accommodation } = models;
const {
Sequelize, Like, Accommodation, Comment
} = models;

/**
* Class representing the accommodation controller
Expand Down Expand Up @@ -68,6 +70,8 @@ class AccommodationController {
static async createAccommodation(req, res) {
try {
const { dataValues } = await Accommodation.create({ ...req.body });
// eslint-disable-next-line no-console
console.log(dataValues);
if (dataValues.id) {
return HelperMethods.requestSuccessful(res, {
success: true,
Expand All @@ -82,6 +86,37 @@ class AccommodationController {
return HelperMethods.serverError(res, 'Something failed');
}
}

/**
* Comment on an Accommodation Facility
* Route: POST: /Accommodation
* @param {object} req - HTTP Request object
* @param {object} res - HTTP Response object
* @return {res} res - HTTP Response object
* @memberof AccommodationController
*/
static async commentOnAccommodation(req, res) {
const { comment, accommodationFacility } = req.body;
const { username, id: userId, profileImage } = req.decoded;
try {
const where = {
userId, username, profileImage, comment, accommodationFacility
};
const isCreated = await Comment.create({ ...where });
if (isCreated) {
return HelperMethods.requestSuccessful(res, {
success: true,
message: 'Comment created successfully',
data: isCreated
});
}
return HelperMethods.serverError(res,
'Could not create comment. Please try again');
} catch (error) {
if (error.errors) return HelperMethods.sequelizeValidationError(res, error);
return HelperMethods.serverError(res, 'Something failed');
}
}
}

export default AccommodationController;
41 changes: 41 additions & 0 deletions src/migrations/04-create-comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('Comments', {
id: {
type: Sequelize.UUID,
primaryKey: true,
defaultValue: Sequelize.UUIDV4,
},
username: {
type: Sequelize.STRING,
allowNull: false,
},
accommodationFacility: {
type: Sequelize.STRING,
allowNull: false,
},
userId: {
type: Sequelize.UUID,
references: {
model: 'Users',
key: 'id'
}
},
comment: {
type: Sequelize.STRING,
allowNull: false,
},
profileImage: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
}),

down: queryInterface => queryInterface.dropTable('Comments')
};
34 changes: 34 additions & 0 deletions src/models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export default (sequelize, DataTypes) => {
const Comment = sequelize.define('Comment', {
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
},
username: {
type: DataTypes.STRING,
allowNull: false,
},
userId: DataTypes.UUID,
accommodationFacility: {
type: DataTypes.STRING,
allowNull: false,
},
comment: {
type: DataTypes.STRING,
allowNull: false,
},
profileImage: {
type: DataTypes.STRING
}
});

Comment.associate = models => {
Comment.belongsTo(models.User, {
foreignKey: 'userId',
onDelete: 'CASCADE'
});
};

return Comment;
};
6 changes: 6 additions & 0 deletions src/routes/accommodationRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ const accommodationRoutes = app => {
Authorization.confirmAdmin,
AccommodationController.createAccommodation
);
app.post(
'/api/v1/accommodation/comment',
Authorization.checkToken,
Validate.commentOnAccommodation,
AccommodationController.commentOnAccommodation
);
};

export default accommodationRoutes;
26 changes: 26 additions & 0 deletions src/seeders/03-demo-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
up: queryInterface => queryInterface.bulkInsert('Comments', [
{
id: '2125be7b-f1f1-4f0a-af86-49c657870b5c',
userId: '3821b930-ce48-4ac8-9ddf-ee3bf7980d08',
username: 'gmemmy',
accommodationFacility: 'Eko Hotels and Suites',
comment: 'The best hotel there is for a barefoot nomad.',
profileImage: 'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwiP',
createdAt: new Date(),
updatedAt: new Date()
},
{
id: '4712fc7e-ca41-457f-872e-4a64b79efbba',
userId: '3821b930-ce48-4ac8-9ddf-ee3bf7980d08',
username: 'JohnDoe',
accommodationFacility: 'Eko Hotels and Suites',
comment: 'A home away from home. Top notch service.',
profileImage: 'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwiP',
createdAt: new Date(),
updatedAt: new Date()
}
], {}),

down: queryInterface => queryInterface.bulkDelete('Comments', null, {})
};
14 changes: 14 additions & 0 deletions src/validation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,20 @@ class Validate {
next();
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
* @param {callback} next - The callback that passes the request to the next handler
* @returns {object} res - Response object when query is invalid
* @memberof Validate
*/
static commentOnAccommodation(req, res, next) {
const { comment, accommodationFacility } = req.body;
if (!accommodationFacility) return allFieldsRequired(res, 'accommodationFacility');
if (!comment) return allFieldsRequired(res, 'comment');
next();
}

/**
* @param {object} req - Request object
* @param {object} res - Response object
Expand Down

0 comments on commit 6a6b62c

Please sign in to comment.