-
Notifications
You must be signed in to change notification settings - Fork 6
#160193526 Fix create article response #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Lumexralph
wants to merge
10
commits into
develop
Choose a base branch
from
bg_fix_create_article_response
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
69ad3d2
bug(article-response): Fix create article response
fc8a275
bug(article-response): Fix create article response
11f17f9
bug(article-response): Fix create article response
6c2aa8c
bug(article-response): Fix create article response
b15b14e
bug(article-response): Fix create article response
5e18b9a
bug(article-response): Fix create article response
a6a7ea1
bug(article-response): Fix create article response
68c26c5
enable ordering of comments and article
Oloyedesinmiloluwa 39dfdb9
bug(article-response): Fix create article response
10f9f1e
Merge branch 'develop' into bg_fix_create_article_response
anneKay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,15 @@ | ||
| import dotenv from 'dotenv'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| const config = { | ||
| secret: process.env.SECRET, | ||
| jwtSecret: process.env.JWT_TOKEN_SECRET, | ||
| cloudinary: { | ||
| cloud_name: process.env.CLOUD_NAME, | ||
| api_key: process.env.API_KEY, | ||
| api_secret: process.env.API_SECRET, | ||
| } | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or 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 hidden or 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 hidden or 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,8 +1,10 @@ | ||
| import { Op } from 'sequelize'; | ||
| import cloudinary from '../config/cloudinary'; | ||
| import Utilities from '../helpers/utilities'; | ||
| import { Article, User, Like, Payment } from '../models'; | ||
| import { | ||
| Article, User, Like, Payment | ||
| } from '../models'; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unexpected line break before this closing brace object-curly-newline |
||
| import createArticleHelper from '../helpers/createArticleHelper'; | ||
| import editResponse from '../helpers/editArticleHelper'; | ||
|
|
||
| /** | ||
| * Article class for users | ||
|
|
@@ -47,35 +49,105 @@ class ArticleController { | |
| * @returns {object} - the found article from database or error if not found | ||
| */ | ||
| static getArticle(req, res, next) { | ||
| const { articleObject } = req; | ||
| if (articleObject.isPaidFor === true) { | ||
| return Payment.find({ | ||
| where: { | ||
| [Op.and]: [ | ||
| { userId: req.userId }, | ||
| { articleId: articleObject.id } | ||
| ] | ||
| const { articleObject, userId } = req; | ||
| const articleBody = `${articleObject.body.substring(0, 500)}...`; | ||
| const notPaid = { | ||
| article: { | ||
| title: articleObject.title, | ||
| slug: articleObject.slug, | ||
| body: articleBody, | ||
| tagList: articleObject.tagList, | ||
| categorylist: articleObject.categorylist, | ||
| imageUrl: articleObject.imageUrl, | ||
| isPaidFor: articleObject.isPaidFor, | ||
| price: articleObject.price, | ||
| readTime: articleObject.readTime, | ||
| createdAt: articleObject.createdAt, | ||
| author: articleObject.author, | ||
| likes: articleObject.likes, | ||
| errors: { | ||
| body: ['You need to purchase this article to read it'] | ||
| } | ||
| }) | ||
| .then((payment) => { | ||
| if (payment) { | ||
| return res.status(200).json({ | ||
| article: articleObject, | ||
| }); | ||
| } | ||
| }; | ||
| if (articleObject.isPaidFor === true) { | ||
| if (userId) { | ||
| return Payment.find({ | ||
| where: { | ||
| [Op.and]: [ | ||
| { userId: req.userId }, | ||
| { articleId: articleObject.id } | ||
| ] | ||
| } | ||
| return res.status(400).json({ | ||
| errors: { | ||
| body: ['You need to purchase this article to read it'] | ||
| } | ||
| }); | ||
| }) | ||
| .catch(next); | ||
| .then((payment) => { | ||
| if (payment || articleObject.author.id === userId) { | ||
| return res.status(200).json({ | ||
| article: articleObject, | ||
| }); | ||
| } | ||
| return res.status(200).json(notPaid); | ||
| }) | ||
| .catch(next); | ||
| } | ||
| return res.status(200).json(notPaid); | ||
| } | ||
| return res.status(200).json({ | ||
| article: articleObject, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * get an article using slug as query parameter | ||
| * @param {object} req - request object | ||
| * @param {object} res - response object | ||
| * @param {function} next - to handle errors | ||
| * @returns {object} - the found article from database or error if not found | ||
| */ | ||
| static getAllUserArticles(req, res, next) { | ||
| const { username } = req.params; | ||
| const { page, limit } = req; | ||
| let offset = null; | ||
|
|
||
| if (page || limit) { | ||
| // calculate offset | ||
| offset = limit * (page - 1); | ||
| } | ||
| return User.findOne({ where: { username, } }) | ||
| .then(user => Article.findAll({ | ||
| where: { | ||
| userId: user.id, | ||
| }, | ||
| include: [{ | ||
| model: User, | ||
| as: 'author', | ||
| attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] } | ||
| }, | ||
| { | ||
| model: Like, | ||
| as: 'likes', | ||
| }], | ||
| offset, | ||
| limit, | ||
| })) | ||
| .then((articles) => { | ||
| if (articles.length === 0) { | ||
| const message = page ? 'articles limit exceeded' | ||
| : 'Sorry, no articles created'; | ||
| return res.status(200).json({ | ||
| message, | ||
| articles, | ||
| articlesCount: articles.length | ||
| }); | ||
| } | ||
| return res.status(200).json({ | ||
| articles, | ||
| articlesCount: articles.length | ||
| }); | ||
| }) | ||
| .catch(next); | ||
| } | ||
|
|
||
| /** | ||
| * get all articles created and use the query | ||
| * if provided to implement pagination | ||
|
|
@@ -90,14 +162,20 @@ class ArticleController { | |
| if (req.query.author || req.query.tag || req.query.title || req.query.category) return next(); | ||
|
|
||
| if (page || limit) { | ||
| // calculate offset | ||
| // calculate offset | ||
| offset = limit * (page - 1); | ||
| } | ||
| return Article | ||
| .findAll({ | ||
| order: [['id', 'DESC']], | ||
| include: [{ | ||
| model: User, | ||
| attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] } | ||
| as: 'author', | ||
| attributes: { exclude: ['id', 'email', 'hashedPassword', 'createdAt', 'updatedAt'] }, | ||
| }, | ||
| { | ||
| model: Like, | ||
| as: 'likes', | ||
| }], | ||
| offset, | ||
| limit, | ||
|
|
@@ -133,31 +211,20 @@ class ArticleController { | |
| * successful requests, or error object for | ||
| * requests that fail | ||
| */ | ||
| static editArticle(req, res, next) { | ||
| static editArticle(req, res) { | ||
| const { | ||
| title, description, body, isPaidFor, price | ||
| title, description, body, isPaidFor, price, imageData, imageUrl, tagList, categorylist, | ||
| } = req.body.article; | ||
| const { count } = req; | ||
| const { slug } = req.params; | ||
| return Article.update({ | ||
| title, | ||
| description, | ||
| body, | ||
| isPaidFor, | ||
| price, | ||
| updatedCount: Utilities.increaseCount(count) | ||
| }, { | ||
| where: { | ||
| slug, | ||
| }, | ||
| returning: true, | ||
| plain: true | ||
| }) | ||
| .then(result => res.status(200).json({ | ||
| success: true, | ||
| article: result[1] | ||
| })) | ||
| .catch(next); | ||
| const articleObject = { | ||
| title, description, body, isPaidFor, price, count, slug, tagList, categorylist, | ||
| }; | ||
| if (imageData) { | ||
| return cloudinary.v2.uploader.upload(imageData, { tags: 'basic_sample' }) | ||
| .then(image => editResponse(res, articleObject, image.url)); | ||
| } | ||
| editResponse(res, articleObject, imageUrl); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unexpected line break after this opening brace object-curly-newline