Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create rating api #30

Merged
Merged
38 changes: 38 additions & 0 deletions backend/controllers/rate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Load required packages
var Rate = require('../models/history.js');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

line 2: variable naming convention should be lowerCamelCase


// Create endpoint /api/ratings for POST
exports.postRates = function (req, res) {
var movieId = req.body.movieId;
var score = req.body.score;
var userId = req.user.id;

Rate.find({
where: {
movie_id: movieId,
user_id: userId
}
}).then(function (rate) {
if (rate) {
rate.updateAttributes({
score: score
}).then(function () {
return res.json({status: 'success', message: 'Ratings Updated'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Capitalised every first letter in message may not be needed

});
} else {
// Save the rating and check for errors
Rate.build({score: score, movie_id: movieId, user_id: userId})
.save().then(function (success) {
res.json({status: 'success', message: 'Ratings Posted!'});
});
}
});
};

// Create endpoint /api/ratings for GET
exports.getRates = function (req, res) {
// Use the Ratings model to find all clients
Rate.findAll({where: {user_id: req.user.id}}).then(function (ratings) {
res.json(ratings);
});
};
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"path": "^0.12.7",
"pg": "^6.1.2",
"pg-hstore": "^2.3.2",
"sequelize": "~2.0.0",
"sequelize": "~2.1.3",
"should": "^11.2.0",
"supertest": "^3.0.0",
"uuid": "^3.0.1"
Expand Down
5 changes: 5 additions & 0 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var authController = require('../controllers/auth');
var oauth2Controller = require('../controllers/oauth2');
var clientController = require('../controllers/client');
var movieController = require('../controllers/movie');
var ratingController = require('../controllers/rate');

// on routes that end in /users
// ----------------------------------------------------
Expand Down Expand Up @@ -75,4 +76,8 @@ router.route('/movies/year')
.get(authController.isAuthenticated,
movieController.getMoviesByProductionYear);

router.route('/ratings')
.post(authController.isAuthenticated, ratingController.postRates)
.get(authController.isAuthenticated, ratingController.getRates);

module.exports = router;