Skip to content

Commit

Permalink
Merge e73103c into cc1cacd
Browse files Browse the repository at this point in the history
  • Loading branch information
eliemugenzi committed Jul 20, 2019
2 parents cc1cacd + e73103c commit 8e0323d
Show file tree
Hide file tree
Showing 71 changed files with 1,578 additions and 1,115 deletions.
6 changes: 6 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ exclude_patterns:
- "src/sequelize/seeders/"
- "**/node_modules/"
- "test/**/*.js"
- "src/middleware/search.js"
- "src/helpers/user/*.js"
- "src/helpers/article/*.js"
- "src/helpers/comment/*.js"
- "src/helpers/validationSchemas.js"
- "src/middleware/shareArticle.js"
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@
"src/sequelize/**/*.js",
"test/**/*.js",
"src/helpers/notifications/*.js",
"src/helpers/SocketIO.js"
"src/helpers/SocketIO.js",
"src/helpers/stats/*.js",
"src/helpers/article/*.js",
"src/middleware/search.js"
]
}
}
141 changes: 141 additions & 0 deletions src/api/controllers/articleBlocksController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
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'
}
});
});
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async getBlockedArticles(req, res) {
const blockedArticles = await BlockedArticles.findAll({});
return res.status(200).json({ status: 200, data: blockedArticles });
}

/**
* @param {object} req
* @param {object} res
* @return {object} returns a message with operation status
*/
static async getReportedArticles(req, res) {
const reportedArticles = await ReportedArticles.findAll({});
res.json({ status: 200, data: reportedArticles });
}
}

export default ArticleBlocks;
67 changes: 67 additions & 0 deletions src/api/controllers/articleDislikesController.js
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;
67 changes: 67 additions & 0 deletions src/api/controllers/articleLikesController.js
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;
Loading

0 comments on commit 8e0323d

Please sign in to comment.