Skip to content

Commit

Permalink
feature/rate-center
Browse files Browse the repository at this point in the history
- write controller for rating center
- write tests for ratings controller
  • Loading branch information
Billmike committed Apr 20, 2018
1 parent 1e77a70 commit 4c59006
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 2 deletions.
85 changes: 85 additions & 0 deletions server/controllers/rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import db from '../models';
import serverError from '../errorHandler/serverError';

const { Center, Rating } = db;

/**
* Controller for handling ratings
*/
class Ratings {
/**
* Rate a center
*
* @param {object} request - The request object
* @param {object} response - The response object
*
* @returns The rated center
*/
static rateCenter(request, response) {
Center.findById(request.params.centerId)
.then((foundCenter) => {
if (!foundCenter) {
return response.status(400).json({
message: 'No center found.'
});
}
return Rating.findAll({
where: {
centerId: request.params.centerId
}
}).then((ratedCenter) => {
if (ratedCenter.length == 0) {
return Rating.create({
rating: request.body.rating,
userId: request.userDetails.id,
centerId: request.params.centerId
}).then((centerRated) => {
return response.status(201).json({
message: 'Successfully rated this center.',
centerDetails: {
centerRated
}
});
});
}
const usersArray = [];
ratedCenter.forEach(center => usersArray
.push(center.dataValues.userId));
if (usersArray.includes(request.userDetails.id)) {
return Rating.findOne({
where: {
userId: request.userDetails.id
}
}).then(updateCenterRating => updateCenterRating
.update({ rating: request.body.rating }))
.then((ratingUpdated) => {
return response.status(201).json({
message: 'Your rating has been updated.',
centerDetails: {
ratingUpdated
}
});
});
}
return Rating.create({
rating: request.body.rating,
userId: request.userDetails.id,
centerId: request.params.centerId
}).then((ratedCenter) => {
return response.status(201).json({
message: '>>>>><<<< Successfully',
centerDetails: {
ratedCenter
}
});
});
});
}).catch(() => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Ratings;
45 changes: 45 additions & 0 deletions server/migrations/20180420014644-create-ratings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Ratings', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
rating: {
type: Sequelize.FLOAT
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
centerId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Centers',
key: 'id',
as: 'centerId'
}
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Users',
key: 'id',
as: 'userId'
}
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Ratings');
}
};
5 changes: 5 additions & 0 deletions server/models/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'venue',
as: 'events'
});

models.Center.hasMany(models.Rating, {
foreignKey: 'centerId',
as: 'centerId'
});
};
return Center;
};
18 changes: 18 additions & 0 deletions server/models/rating.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = (sequelize, DataTypes) => {
const Rating = sequelize.define('Rating', {
rating: {
type: DataTypes.FLOAT,
defaultValue: 0
}
});
Rating.associate = (models) => {
models.Rating.belongsTo(models.Center, {
foreignKey: 'centerId',
onDelete: 'cascade'
});
models.Rating.belongsTo(models.User, {
foreignKey: 'userId'
});
};
return Rating;
};
5 changes: 5 additions & 0 deletions server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'organizer',
as: 'events'
});

models.User.hasMany(models.Rating, {
foreignKey: 'userId',
as: 'userId'
});
};

return User;
Expand Down
10 changes: 8 additions & 2 deletions server/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import userController from '../controllers/user';
import eventController from '../controllers/event';
import centerController from '../controllers/center';
import ratingController from '../controllers/rating';
import sessionControl from '../middleware/SessionControl';
import eventController from '../controllers/event';
import userController from '../controllers/user';

module.exports = (app) => {
app.get('/api', (request, response) =>
Expand Down Expand Up @@ -55,4 +56,9 @@ module.exports = (app) => {
sessionControl.isLoggedIn, sessionControl.isUser,
userController.modifyUserDetails
);
app.post(
'/api/v1/center/rating/:centerId',
sessionControl.isLoggedIn, sessionControl.isUser,
ratingController.rateCenter
);
};
1 change: 1 addition & 0 deletions server/tests/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './user.test';
import './center.test';
import './event.test';
import './rating.test';
45 changes: 45 additions & 0 deletions server/tests/rating.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from 'chai';
import supertest from 'supertest';
import app from '../app';
import dummyUser from './seed/userseed';

const request = supertest(app);

describe('Integration tests for Ratings functionality', () => {
it('should create a rating for a center', (done) => {
request.post(`/api/v1/center/rating/1?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(4)
.end((error, response) => {
expect(response.status).to.equal(201);
done();
});
});
it(
'should update the ratings of a user who has rated the same center previously',
(done) => {
request.post(`/api/v1/center/rating/1?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(3.5)
.end((error, response) => {
expect(response.status).to.equal(201);
done();
});
}
);
it('should fail if the center is not found', (done) => {
request.post(`/api/v1/center/rating/100?token=${dummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(3)
.end((error, response) => {
expect(response.status).to.equal(400);
done();
});
});
});

0 comments on commit 4c59006

Please sign in to comment.