Skip to content

Commit

Permalink
feature[get user articles]: Enables registered user to get thier arti…
Browse files Browse the repository at this point in the history
…cles

Get All user authored articles they functionality

[Finishes #168252582]
  • Loading branch information
Adekoreday committed Sep 4, 2019
1 parent 5cdf538 commit 5fa3735
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 81 deletions.
32 changes: 32 additions & 0 deletions server/controllers/Articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,38 @@ const createLikeOrDislike = async (userAction, userId, article) => {
return serverError(res);
}
}

/**
* gets all author's article
*
* @name userArticles
* @async
* @static
* @memberof Articles
* @param {Object} req express request object
* @param {Object} res express response object
* @returns {JSON} Details of the users article
*/
static async userArticles(req, res) {
try {
const userArticle = await Article.findAndCountAll({
where: {
authorId: req.user.id
}
});
const total = userArticle.count;
const data = userArticle.rows;
const articles = {
total,
data
};
res.status(200).send({
articles
});
} catch (error) {
return serverError(res);
}
}
}

export default Articles;
2 changes: 1 addition & 1 deletion server/database/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default (sequelize, DataTypes) => {
type: DataTypes.STRING,
allowNull: true,
defaultValue:
'http://res.cloudinary.com/teamrambo50/image/upload/v1565884519/fazaithupzfod35wxwys.png',
'http://res.cloudinary.com/teamrambo50/image/upload/v1567524113/pgkcpg4prdi5p2fpiorq.png',
validate: {
isUrl: {
msg: 'image must be a valid url path'
Expand Down
227 changes: 148 additions & 79 deletions server/docs/authors-haven-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -795,43 +795,43 @@ paths:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'
delete:
summary: Remove like from an article
description: Endpoint for removing a like on an article
security:
- ApiKeyAuth: []
parameters:
- name: slug
in: path
required: true
description: Unique article slug
schema:
type: string
responses:
200:
description: article like removed successfully
content:
application/json:
schema:
'$ref': '#/components/schemas/removeArticleLikeDislikeResponse'
404:
description: Article not found
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
401:
description: Authorization token not provided
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
500:
description: Unexpected internal server error
content:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'
delete:
summary: Remove like from an article
description: Endpoint for removing a like on an article
security:
- ApiKeyAuth: []
parameters:
- name: slug
in: path
required: true
description: Unique article slug
schema:
type: string
responses:
200:
description: article like removed successfully
content:
application/json:
schema:
'$ref': '#/components/schemas/removeArticleLikeDislikeResponse'
404:
description: Article not found
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
401:
description: Authorization token not provided
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
500:
description: Unexpected internal server error
content:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'

/api/v1/articles/{slug}/dislike:
post:
Expand Down Expand Up @@ -877,43 +877,43 @@ paths:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'
delete:
summary: Remove dislike from an article
description: Endpoint for removing a dislike on an article
security:
- ApiKeyAuth: []
parameters:
- name: slug
in: path
required: true
description: Unique article slug
schema:
type: string
responses:
200:
description: article dislike removed successfully
content:
application/json:
schema:
'$ref': '#/components/schemas/removeArticleLikeDislikeResponse'
404:
description: Article not found
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
401:
description: Authorization token not provided
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
500:
description: Unexpected internal server error
content:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'
delete:
summary: Remove dislike from an article
description: Endpoint for removing a dislike on an article
security:
- ApiKeyAuth: []
parameters:
- name: slug
in: path
required: true
description: Unique article slug
schema:
type: string
responses:
200:
description: article dislike removed successfully
content:
application/json:
schema:
'$ref': '#/components/schemas/removeArticleLikeDislikeResponse'
404:
description: Article not found
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
401:
description: Authorization token not provided
content:
application/json:
schema:
'$ref': '#/components/schemas/errorResponse'
500:
description: Unexpected internal server error
content:
application/json:
schema:
'$ref': '#/components/schemas/serverResponse'
/api/v1/articles/update/{slug}:
patch:
summary: A user can update article
Expand Down Expand Up @@ -1052,7 +1052,29 @@ paths:
content:
application/json:
schema:
"$ref": "#/components/schemas/serverResponse"
"$ref": "#/components/schemas/serverResponse"

api/v1/articles/getUserArticles:
get:
summary: Get all user articles
description: Get all user articles and counts
security:
- ApiKeyAuth: []
responses:
'200':
description: request successful
content:
application/json:
schema:
"$ref": "#/components/schemas/alluserArticleResponse"
'401':
description: request unsuccessful
content:
application/json:
schema:
"$ref": "#/components/schemas/alluserError"




components:
Expand Down Expand Up @@ -1494,10 +1516,6 @@ components:
type: string
format: date-time
example: 2017-07-21T17:32:28Z
createdAt:
type: string
format: date-time
example: 2017-07-21T17:32:28Z
likes:
type: array
items:
Expand Down Expand Up @@ -1587,3 +1605,54 @@ components:
status:
type: string
example: status is required
alluserArticleResponse:
type: object
properties:
myarticle:
type: object
properties:
count:
type: integer
rows:
type: array
items:
type: object
properties:
id:
type: integer
slug:
type: string
title:
type: string
description:
type: string
image:
type: string
articleBody:
type: string
authorId:
type: integer
categoryId:
type: integer
likesCount:
type: integer
dislikesCount:
type: integer
publishedAt:
type: string
format: date-time
example: 2017-07-21T17:32:28Z
isArchived:
type: boolean
createdAt:
type: integer
format: date-time
updatedAt:
type: integer
format: date-time
alluserError:
type: object
properties:
message:
type: string
example: no token provided
8 changes: 8 additions & 0 deletions server/routes/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,12 @@ router.patch(
articleEditValidation,
Articles.update
);

router.get(
'/user/articles',
verifyToken,
getSessionFromToken,
checkUserVerification,
Articles.userArticles
);
export default router;
74 changes: 74 additions & 0 deletions test/articles/getUserArticles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import chai, { expect } from 'chai';
import chaiHttp from 'chai-http';
import sinon from 'sinon';
import app from '../../server';
import { getNewUser } from '../users/__mocks__';
import model from '../../server/database/models';
import { ArticleData4, ArticleData20 } from './__mocks__';

chai.use(chaiHttp);
const { BASE_URL } = process.env;
let userToken;
const userSignUp = getNewUser();
const { Article } = model;
before(async () => {
await chai
.request(app)
.post(`${BASE_URL}/users/create`)
.send({ ...userSignUp, confirmPassword: userSignUp.password });
const response = await chai
.request(app)
.post(`${BASE_URL}/sessions/create`)
.send({ userLogin: userSignUp.email, password: userSignUp.password });
userToken = response.body.token;

await chai
.request(app)
.post(`${BASE_URL}/articles/create`)
.set('Authorization', userToken)
.send(ArticleData4);
await chai
.request(app)
.post(`${BASE_URL}/articles/create`)
.set('Authorization', userToken)
.send(ArticleData20);
});

describe('get User Article Test', () => {
context('when a user enters a valid token', () => {
it('get all his article successfully', async () => {
const response = await chai
.request(app)
.get(`${BASE_URL}/articles/user/articles`)
.set('Authorization', userToken);
expect(response.body.articles.total).to.equal(2);
expect(response.status).to.equal(200);
});
});

context('when a user enter an invalid token', () => {
it('returns an error', async () => {
const response = await chai
.request(app)
.get(`${BASE_URL}/articles/user/articles`);
expect(response.status).to.equal(401);
});
});

context(
'when the get User Articles method encounters a internal error',
() => {
it('throws a server error', async () => {
const userArticles = sinon
.stub(Article, 'findAndCountAll')
.callsFake(() => {});
const response = await chai
.request(app)
.get(`${BASE_URL}/articles/user/articles`)
.set('Authorization', userToken);
expect(response.status).to.equal(500);
userArticles.restore();
});
}
);
});
Loading

0 comments on commit 5fa3735

Please sign in to comment.