This repository has been archived by the owner on Aug 16, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Remove clamp - serious performance hit in Firefox and Edge * Rough styles and layout * Fix position * Shouldn't be absolute * Clean * Use LoginLink component in top nav * Clean up line endings * Don't extend airbnb eslint config * Unnecessary rule
- Loading branch information
1 parent
5d3b5e1
commit e9ea574
Showing
34 changed files
with
1,037 additions
and
848 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
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 |
---|---|---|
@@ -1,81 +1,74 @@ | ||
const express = require('express'); | ||
const passport = require('passport'); | ||
const Rating = require('../models/Rating'); | ||
const User = require('../models/User'); | ||
|
||
const router = express.Router(); | ||
|
||
router.get('/', (req, res) => { | ||
const { | ||
mediaType, tmdbid, seasonNum, episodeNum, | ||
} = req.query; | ||
if (episodeNum && !seasonNum) { | ||
return res.status(404).json('Ratings not found, please specify a season number as well'); | ||
} | ||
return Rating.find({ | ||
mediaType, tmdbid, seasonNum, episodeNum, | ||
}).populate('user', 'username') | ||
.then(ratings => res.json(ratings)); | ||
}); | ||
|
||
router.post('/add', passport.authenticate('jwt', { session: false }), (req, res) => { | ||
const { | ||
user, | ||
body: { | ||
mediaType, tmdbid, seasonNum, episodeNum, value, | ||
}, | ||
} = req; | ||
// return error if score invalid | ||
if (value > 5 || value < 0) { | ||
return res.status(400).json({ error: 'Rating couldn\'t be added to the database' }); | ||
res.status(400).json({ error: 'Rating couldn\'t be added to the database' }); | ||
} | ||
return Rating | ||
.findOne({ | ||
user, | ||
mediaType, | ||
tmdbid, | ||
seasonNum, | ||
episodeNum, | ||
}) | ||
.then((rating) => { | ||
if (rating) { | ||
if (parseInt(value, 10) === 0) { | ||
return Rating | ||
.findOne({ | ||
user, | ||
mediaType, | ||
tmdbid, | ||
seasonNum, | ||
episodeNum, | ||
}) | ||
.remove() | ||
.then(() => { | ||
User.update({ _id: user.id }, { $pull: { ratings: rating.id } }) | ||
.then(() => res.json({})); | ||
}); | ||
} | ||
return Rating.findOneAndUpdate({ | ||
user, | ||
mediaType, | ||
tmdbid, | ||
seasonNum, | ||
episodeNum, | ||
}, { score: value }, { new: true }) | ||
.then(newRating => res.json(newRating)); | ||
// find the rating | ||
Rating.findOne({ | ||
user, | ||
mediaType, | ||
tmdbid, | ||
seasonNum, | ||
episodeNum, | ||
}).then((rating) => { | ||
if (rating) { | ||
if (parseInt(value, 10) === 0) { | ||
// remove rating if score is 0 | ||
rating.remove().then(() => { | ||
// remove rating id from user | ||
user.update({ $pull: { ratings: rating.id } }).then(() => { | ||
// return empty object | ||
res.json({}); | ||
}); | ||
}); | ||
} else { | ||
// update the score for the rating | ||
rating.update({ $set: { score: value } }).then(() => { | ||
// return the rating with the new score | ||
res.json({ | ||
mediaType: rating.mediaType, | ||
tmdbid: rating.tmdbid, | ||
score: parseInt(value, 10), | ||
seasonNum: rating.seasonNum || undefined, | ||
episodeNum: rating.episodeNum || undefined, | ||
}); | ||
}); | ||
} | ||
|
||
return new Rating({ | ||
} else { | ||
// create a new rating | ||
new Rating({ | ||
user, | ||
mediaType, | ||
tmdbid, | ||
seasonNum, | ||
episodeNum, | ||
score: value, | ||
}) | ||
.save() | ||
.then(newRating => | ||
User.update({ _id: user.id }, { $push: { ratings: { _id: newRating.id } } }) | ||
.then(() => res.json(newRating))); | ||
}); | ||
}).save().then(newRating => | ||
// add the rating id to the user | ||
user.update({ $push: { ratings: { _id: newRating.id } } }).then(() => { | ||
// return the new rating | ||
res.json({ | ||
mediaType: newRating.mediaType, | ||
tmdbid: newRating.tmdbid, | ||
score: newRating.score, | ||
seasonNum: newRating.seasonNum || undefined, | ||
episodeNum: newRating.episodeNum || undefined, | ||
}); | ||
})); | ||
} | ||
}); | ||
}); | ||
|
||
module.exports = router; |
Oops, something went wrong.