-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(refactoring): Improve code maintainability
- Loading branch information
Elie Mugenzi
authored and
Elie Mugenzi
committed
Jul 4, 2019
1 parent
ea8a15c
commit 93a6ed0
Showing
27 changed files
with
609 additions
and
522 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import models from '../../sequelize/models'; | ||
|
||
const { | ||
Article, BlockedArticles, ReportedArticles, User | ||
} = models; | ||
|
||
// eslint-disable-next-line no-array-constructor | ||
const days = new Array( | ||
'Monday', | ||
'Tuesday', | ||
'Wednesday', | ||
'Thursday', | ||
'Friday', | ||
'Saturday', | ||
'Sunday' | ||
); | ||
|
||
/** | ||
* @class | ||
*/ | ||
class ArticleBlocks { | ||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async reportArticle(req, res) { | ||
const { username } = req.user; | ||
const { comment } = req.body; | ||
const { slug } = req.params; | ||
ReportedArticles.create({ | ||
slug, | ||
comment, | ||
username, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}).then((out) => { | ||
res.status(201).send({ | ||
status: 201, | ||
data: out | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async blockArticle(req, res) { | ||
const { slug } = req.params; | ||
const { user } = req; | ||
const { description } = req.body; | ||
const article = await Article.findOne({ where: { slug } }); | ||
const reporterUsername = await ReportedArticles.findOne({ | ||
where: { slug } | ||
}); | ||
const username = !reporterUsername | ||
? null | ||
: reporterUsername.dataValues.username; | ||
const repoterId = username === null ? null : await User.findOne({ where: { username } }); | ||
const id = repoterId === null ? null : repoterId.dataValues.id; | ||
const object = { | ||
reporterId: id, | ||
articleId: article.id, | ||
authorId: article.authorId, | ||
moderatorId: user.id, | ||
blockedDay: days[new Date().getDay() - 1], | ||
description | ||
}; | ||
BlockedArticles.create(object).then(async (responce) => { | ||
await Article.update( | ||
{ blocked: true }, | ||
{ where: { id: responce.articleId } } | ||
); | ||
res.status(201).send({ | ||
status: 201, | ||
data: { | ||
message: 'Article blocked successfully', | ||
responce | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async unBlockArticle(req, res) { | ||
const { slug } = req.params; | ||
const { id } = await Article.findOne({ where: { slug } }); | ||
BlockedArticles.destroy({ where: { articleId: id } }).then(async () => { | ||
await Article.update({ blocked: false }, { where: { slug } }); | ||
res.status(200).send({ | ||
status: 200, | ||
data: { | ||
message: 'Article unblocked successfully' | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default ArticleBlocks; |
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,148 @@ | ||
import models from '../../sequelize/models'; | ||
import updateLike from '../../helpers/article/updateLike'; | ||
import updateDislike from '../../helpers/article/updateDislike'; | ||
|
||
const { LikeDislike } = models; | ||
/** | ||
* @class | ||
*/ | ||
class ArticleLikes { | ||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async likeArticle(req, res) { | ||
const { id: currentUser } = req.user; | ||
const { slug } = req.params; | ||
|
||
try { | ||
const foundArticle = req.article; | ||
const hasDisliked = await LikeDislike.findAll({ | ||
where: { | ||
articleId: foundArticle.id, | ||
userId: currentUser, | ||
dislikes: 1 | ||
} | ||
}); | ||
|
||
// If user has disliked before, remove dislike, add like. | ||
if (hasDisliked[0]) { | ||
await updateLike(hasDisliked[0].id); | ||
return res | ||
.status(200) | ||
.json({ message: `User ${currentUser} has liked article ${slug}` }); | ||
} | ||
|
||
// the user hasn't liked or disliked before, create new like | ||
await LikeDislike.create({ | ||
userId: currentUser, | ||
articleId: foundArticle.id, | ||
dislikes: 0, | ||
likes: 1 | ||
}); | ||
|
||
return res | ||
.status(200) | ||
.json({ message: `User ${currentUser} has liked article ${slug}` }); | ||
} catch (error) { | ||
return res.status(500).json({ error: `${error}` }); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async dislikeArticle(req, res) { | ||
const { id: currentUser } = req.user; | ||
const { slug } = req.params; | ||
|
||
try { | ||
const foundArticle = req.article; | ||
|
||
// Check the current user has not liked or disliked this article before | ||
const hasLiked = await LikeDislike.findAll({ | ||
where: { | ||
articleId: foundArticle.id, | ||
userId: currentUser, | ||
likes: 1 | ||
} | ||
}); | ||
|
||
// If user has liked before, remove like, add dislike. | ||
if (hasLiked[0]) { | ||
updateDislike(hasLiked[0].id); | ||
return res.status(200).json({ | ||
message: `User ${currentUser} has disliked article ${slug}` | ||
}); | ||
} | ||
|
||
// the user hasn't disliked before, create new dislike | ||
await LikeDislike.create({ | ||
userId: currentUser, | ||
articleId: foundArticle.id, | ||
dislikes: 1, | ||
likes: 0 | ||
}); | ||
|
||
return res | ||
.status(200) | ||
.json({ message: `User ${currentUser} has disliked article ${slug}` }); | ||
} catch (error) { | ||
return res.status(500).json({ error: `${error}` }); | ||
} | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async getLikes(req, res) { | ||
const { slug } = req.params; | ||
const foundArticle = req.article; | ||
|
||
// Get likes | ||
const likeCount = await LikeDislike.count({ | ||
where: { articleId: foundArticle.id, likes: 1 } | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: 200, | ||
data: { | ||
articleSlug: slug, | ||
numberOfLikes: likeCount | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* @param {object} req | ||
* @param {object} res | ||
* @return {object} returns a message with operation status | ||
*/ | ||
static async getDislikes(req, res) { | ||
const { slug } = req.params; | ||
const foundArticle = req.article; | ||
|
||
// Get likes | ||
const likeCount = await LikeDislike.count({ | ||
where: { | ||
articleId: foundArticle.id, | ||
dislikes: 1 | ||
} | ||
}); | ||
|
||
return res.status(200).json({ | ||
status: 200, | ||
data: { | ||
articleSlug: slug, | ||
numberOfDislikes: likeCount | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default ArticleLikes; |
Oops, something went wrong.