-
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 9, 2019
1 parent
fcb8c10
commit dc5d841
Showing
50 changed files
with
990 additions
and
679 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,121 @@ | ||
import models from '../../sequelize/models'; | ||
|
||
import AuthorNotifier from '../../helpers/NotifyAuthorOnArticleBlock'; | ||
|
||
const { | ||
notifyAuthorblock, notifyAuthorUnblock | ||
} = AuthorNotifier; | ||
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 { dataValues: { email, lastName } } = await User.findOne({ | ||
where: { id: article.authorId } | ||
}); | ||
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] || 'Sunday', | ||
description | ||
}; | ||
BlockedArticles.create(object).then(async (responce) => { | ||
await Article.update( | ||
{ blocked: true }, | ||
{ where: { id: responce.articleId } } | ||
); | ||
await notifyAuthorblock({ email, lastName }); | ||
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, authorId } = await Article.findOne({ where: { slug } }); | ||
|
||
const { dataValues: { email, lastName } } = await User.findOne({ | ||
where: { id: authorId } | ||
}); | ||
|
||
BlockedArticles.destroy({ where: { articleId: id } }).then(async () => { | ||
await Article.update({ blocked: false }, { where: { slug } }); | ||
await notifyAuthorUnblock({ email, lastName, 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,67 @@ | ||
import db from '../../sequelize/models'; | ||
import updateDislike from '../../helpers/article/updateDislike'; | ||
import hasLiked from '../../helpers/article/hasLiked'; | ||
import dislikesCount from '../../helpers/article/countDislikes'; | ||
|
||
const { LikeDislike } = db; | ||
/** | ||
* @class | ||
*/ | ||
class ArticleDislikes { | ||
/** | ||
* @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; | ||
|
||
// If user has liked before, remove like, add dislike. | ||
const { id } = hasLiked(foundArticle.id, currentUser); | ||
if (id) { | ||
updateDislike(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 getDislikes(req, res) { | ||
const { slug } = req.params; | ||
const foundArticle = req.article; | ||
|
||
return res.json({ | ||
status: 200, | ||
data: { | ||
slug, | ||
dislikes: dislikesCount(foundArticle.id) | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default ArticleDislikes; |
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,67 @@ | ||
import models from '../../sequelize/models'; | ||
import updateLike from '../../helpers/article/updateLike'; | ||
import hasDisliked from '../../helpers/article/hasDisliked'; | ||
import likesCount from '../../helpers/article/countLikes'; | ||
|
||
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; | ||
|
||
// If user has disliked before, remove dislike, add like. | ||
const { id } = hasDisliked(foundArticle.id, currentUser); | ||
if (id) { | ||
await updateLike(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 getLikes(req, res) { | ||
const { slug } = req.params; | ||
const foundArticle = req.article; | ||
|
||
return res.status(200).json({ | ||
status: 200, | ||
data: { | ||
articleSlug: slug, | ||
numberOfLikes: likesCount(foundArticle.id) | ||
} | ||
}); | ||
} | ||
} | ||
|
||
export default ArticleLikes; |
Oops, something went wrong.