Skip to content

Commit

Permalink
Merge f33b1f9 into 300f0cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Dubby20 committed Mar 26, 2019
2 parents 300f0cc + f33b1f9 commit 2873a51
Show file tree
Hide file tree
Showing 11 changed files with 596 additions and 153 deletions.
137 changes: 134 additions & 3 deletions controllers/articles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Article, User, Ratings } from '../models';
import {
Article,
Reaction,
User,
Ratings
} from '../models';
import Paginate from '../helpers/paginate';

/**
* @class ArticleController
* @override
Expand Down Expand Up @@ -147,6 +151,134 @@ export default class ArticleController {
}
}

/**
* @desc check if a user likes an article
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof ArticleController
* @return {Object} returns an object
*/
static async likeArticle(req, res) {
const { id: userId } = req.user;
const { slug: articleSlug } = req.params;

const getArticles = await Article.findOne({
where: {
slug: articleSlug
},
});
try {
const likeArticle = await Reaction.findOne({
where: {
articleSlug,
userId,
},
});
if (!likeArticle) {
await Reaction.create({
articleSlug,
userId,
likes: true,
});
const allLikes = await Reaction.findAndCountAll({
where: {
articleSlug,
userId,
likes: true
},
});

return res.status(201).json({
success: true,
message: 'You have liked this article',
getArticles,
likes: allLikes.count
});
}

await Reaction.destroy({
where: {
articleSlug,
userId,
}
});

return res.status(200).json({
success: true,
message: 'You have unliked this article',
getArticles
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error, error.message]
});
}
}

/**
* @desc check if a user dislikes an article
* @param {Object} req - the request object
* @param {Object} res - the response object
* @memberof ArticleController
* @return {Object} returns an object
*/
static async dislikeArticle(req, res) {
const { id: userId } = req.user;
const { slug: articleSlug } = req.params;
const getArticles = await Article.findOne({
where: {
slug: articleSlug
},
});
try {
const likeArticle = await Reaction.findOne({
where: {
articleSlug,
userId
},
});
if (!likeArticle) {
await Reaction.create({
articleSlug,
userId,
likes: false
});
const allDisLikes = await Reaction.findAndCountAll({
where: {
articleSlug,
userId,
likes: false
},
});

return res.status(201).json({
success: true,
message: 'Article disliked successfully',
getArticles,
dislikes: allDisLikes.count
});
}

await Reaction.destroy({
where: {
articleSlug,
userId,
}
});
return res.status(200).json({
success: true,
message: 'You have removed the dislike on this article',
getArticles
});
} catch (error) {
return res.status(500).json({
success: false,
errors: [error.message]
});
}
}

/**
* @description - Search for articles
* @static
Expand Down Expand Up @@ -256,7 +388,6 @@ export default class ArticleController {
attributes: ['username', 'email', 'name', 'bio'],
}]
});

if (req.user) {
const { id } = req.user;
const viewer = await User.findOne({ where: { id } });
Expand Down
15 changes: 9 additions & 6 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable camelcase */
import bcrypt from 'bcrypt';
import dotenv from 'dotenv';
import jwt from 'jsonwebtoken';
Expand Down Expand Up @@ -113,10 +114,10 @@ export default class UserController {
*/
static verifyAccount(req, res) {
const {
params: { verificationId }
params: { verification_id }
} = req;
User.findOne({
where: { verificationId }
where: { verificationId: verification_id }
}).then((foundUser) => {
if (foundUser) {
return foundUser
Expand Down Expand Up @@ -186,9 +187,11 @@ export default class UserController {
* @returns {object} response
*/
static async resetPassword(req, res) {
const { params: { passwordResetToken } } = req;
const { params: { password_reset_token } } = req;
const { password } = req.body;
const getPasswordResetToken = await User.findOne({ where: { passwordResetToken } });
const getPasswordResetToken = await User.findOne(
{ where: { passwordResetToken: password_reset_token } }
);
if (!getPasswordResetToken) {
return res.status(404).json({
success: false,
Expand All @@ -206,9 +209,9 @@ export default class UserController {
await User.update(
{
password,
passwordResetToken: ''
password_reset_token: ''
},
{ where: { passwordResetToken } }
{ where: { passwordResetToken: password_reset_token } }
);
} catch (error) {
return res.status(500).json({
Expand Down
97 changes: 95 additions & 2 deletions doc.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
"description": "Authentication error"
}
}

},
"patch": {
"tags": [
Expand Down Expand Up @@ -442,7 +443,99 @@
"description": "Permission denied, user cannot rate their own article"
}
}
}
},
"/like_article/:slug": {
"post": {
"tags": [
"Articles"
],
"summary": "Like an article",
"description": "Like an article",
"operationId": "likeArticle",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "An article slug used to like the article",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "like",
"description": "Like an article",
"required": true,
"schema": {
"$ref": "#/definitions/rating"
}
}
],
"responses": {
"201": {
"description": "Article liked successfully"
},
"404": {
"description": "Article not found"
},
"200": {
"description": "You have unliked this article"
}
}
}
},
"/dislike_article/:slug": {
"post": {
"tags": [
"Articles"
],
"summary": "Dislike an article",
"description": "Dislike an article",
"operationId": "DislikeArticle",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"parameters": [
{
"name": "authorization",
"in": "header",
"description": "An article slug used to like the article",
"required": true,
"type": "string"
},
{
"in": "body",
"name": "dislike",
"description": "dislike an article",
"required": true,
"schema": {
"$ref": "#/definitions/rating"
}
}
],
"responses": {
"201": {
"description": "Article disliked successfully"
},
"404": {
"description": "Article not found"
},
"200": {
"description": "You have removed the dislike on this article"
}
}
}
},
"/articles/:slug": {
"put": {
"tags": [
"Articles"
Expand Down Expand Up @@ -562,7 +655,6 @@
}
}
}
}
},
"/articles/{slug}/comments": {
"post": {
Expand Down Expand Up @@ -607,6 +699,7 @@
"description": "Incomplete request body"
}
}
}
},
"get": {
"tags": [
Expand Down Expand Up @@ -726,7 +819,6 @@
}
}
}
}
},
"definitions": {
"user": {
Expand Down Expand Up @@ -817,4 +909,5 @@
"description": "Find out more about Swagger",
"url": "http://swagger.io"
}
}
}
Loading

0 comments on commit 2873a51

Please sign in to comment.