-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
356 additions
and
71 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 |
---|---|---|
|
@@ -3,7 +3,7 @@ logs | |
*.log | ||
.DS_Store | ||
.nyc_output | ||
.vs | ||
.vs/ | ||
|
||
npm-debug.log* | ||
|
||
|
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import models from '../models'; | ||
|
||
const { Article, Rating } = models; | ||
/** | ||
* This class contains all the methods responsible for rating | ||
* articles on the app | ||
* It is made up static methods which can be called from anywhere in the app. | ||
*/ | ||
export default class RatingController { | ||
/** | ||
* Prep the request that is sent and get the information needed to rate | ||
* the article. | ||
* @param {object} req the request object | ||
* @param {object} res the response object | ||
*/ | ||
static prepRating(req, res) { | ||
const { id: userId, username } = req.user; | ||
const { slug, rating, author } = req.params; | ||
if (username === author) { | ||
return res.status(403).send({ | ||
status: 'error', | ||
errors: { | ||
user: ['You cannot rate your own article'] | ||
} | ||
}); | ||
} | ||
let star = rating < 1 ? 1 : rating; | ||
star = star > 5 ? 5 : star; | ||
return { | ||
userId, slug, star | ||
}; | ||
} | ||
|
||
/** | ||
* Prepares the response to be sent to the user. | ||
* @param {object} rating the rating object returned from the database | ||
* @param {object} res the response object | ||
* @param {boolean} updated a boolean that indicates if it is a new rating or an | ||
* updated rating (used to set the appropriate status code) | ||
*/ | ||
static ratingResponse(rating, res, updated = false) { | ||
const statusCode = updated ? 200 : 201; | ||
const { | ||
userId, value, slug | ||
} = rating.dataValues; | ||
return res.status(statusCode).send({ | ||
status: 'success', | ||
rating: { | ||
userId, value, slug | ||
}, | ||
}); | ||
} | ||
|
||
/** | ||
* Rate an article and return the rating (number of stars). | ||
* @param {object} req the request object | ||
* @param {object} res the response object | ||
*/ | ||
static rateArticle(req, res) { | ||
const ratingInfo = RatingController.prepRating(req, res); | ||
const { userId, slug, star } = ratingInfo; | ||
if (!slug) return; | ||
Article.findOne({ | ||
where: { slug }, | ||
attributes: ['id'], | ||
}) | ||
.then((article) => { | ||
if (!article) { | ||
return res.status(404).send({ | ||
status: 'error', | ||
errors: { | ||
article: ['No article with the given URL. Please check the URL again'] | ||
} | ||
}); | ||
} | ||
const { id: articleId } = article.dataValues; | ||
Rating.findOne({ where: { articleId, userId } }) | ||
.then((rating) => { | ||
if (!rating) { | ||
return Rating.create({ userId, articleId, value: star }) | ||
.then(newRating => RatingController.ratingResponse(newRating, res)); | ||
} | ||
return Rating.update({ value: star }, { | ||
returning: true, | ||
where: { articleId, userId }, | ||
}) | ||
.then(([, [updatedRating]]) => { | ||
RatingController.ratingResponse(updatedRating, res, true); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |
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,26 +1,30 @@ | ||
import { Router } from 'express'; | ||
import ArticleController from '../controllers/ArticleController'; | ||
import userAuthenticate from '../middlewares/isLoggedIn'; | ||
import isLoggedIn from '../middlewares/isLoggedIn'; | ||
import ArticleValidation from '../middlewares/validations/ArticleValidation'; | ||
import CommentValidation from '../middlewares/validations/CommentValidation'; | ||
import CommentController from '../controllers/CommentController'; | ||
import RatingController from '../controllers/RatingController'; | ||
|
||
const articleRouter = Router(); | ||
|
||
articleRouter.post('/', userAuthenticate, ArticleValidation.validateCreateArticle, ArticleController.createArticle); | ||
articleRouter.post('/', isLoggedIn, ArticleValidation.validateCreateArticle, ArticleController.createArticle); | ||
|
||
articleRouter.put('/:slug', userAuthenticate, ArticleValidation.validateUpdateArticle, ArticleController.updateArticle); | ||
articleRouter.put('/:slug', isLoggedIn, ArticleValidation.validateUpdateArticle, ArticleController.updateArticle); | ||
|
||
articleRouter.delete('/:slug', userAuthenticate, ArticleController.removeArticle); | ||
articleRouter.delete('/:slug', isLoggedIn, ArticleController.removeArticle); | ||
|
||
articleRouter.post('/:slug/comments', userAuthenticate, CommentValidation.validateComment, CommentController.createComment); | ||
articleRouter.post('/:slug/comments', isLoggedIn, CommentValidation.validateComment, CommentController.createComment); | ||
|
||
articleRouter.get('/:slug/comments', userAuthenticate, CommentController.getComments); | ||
articleRouter.get('/:slug/comments', isLoggedIn, CommentController.getComments); | ||
|
||
articleRouter.get('/:slug/comments/:id', userAuthenticate, CommentController.getComment); | ||
articleRouter.get('/:slug/comments/:id', isLoggedIn, CommentController.getComment); | ||
|
||
articleRouter.put('/:slug/comments/:id', userAuthenticate, CommentValidation.validateComment, CommentController.updateComment); | ||
articleRouter.put('/:slug/comments/:id', isLoggedIn, CommentValidation.validateComment, CommentController.updateComment); | ||
|
||
articleRouter.delete('/:slug/comments/:id', isLoggedIn, CommentController.deleteComment); | ||
|
||
articleRouter.post('/:author/:slug/:rating', isLoggedIn, RatingController.rateArticle); | ||
|
||
articleRouter.delete('/:slug/comments/:id', userAuthenticate, CommentController.deleteComment); | ||
|
||
export default articleRouter; |
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
Oops, something went wrong.