-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Billmike/feature/rate-center
feature/rate-center
- Loading branch information
Showing
8 changed files
with
212 additions
and
2 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,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; |
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,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'); | ||
} | ||
}; |
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,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; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import './user.test'; | ||
import './center.test'; | ||
import './event.test'; | ||
import './rating.test'; |
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,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(); | ||
}); | ||
}); | ||
}); |