Skip to content

Commit

Permalink
[feature #166239850] User should be able to see popular authors
Browse files Browse the repository at this point in the history
  • Loading branch information
NedyUdombat committed May 24, 2019
1 parent a0219b9 commit 5e05944
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 1 deletion.
208 changes: 208 additions & 0 deletions src/db/seeders/20190410232221-comment-seeder.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,214 @@ export default {
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d86',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d85',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here too too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d84',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d83',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d82',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d81',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '08fd662d-ed92-419e-8af9-41afd3fb3d80',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '18fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: 'cd75c9de-324e-4b7e-be68-64c0ce09bd4d',
body: 'Description goes here',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '28fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '38fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '20fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '21fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '22fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '23fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '24fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '25fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '26fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '27fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '47d790b9-9995-40df-a1e6-c3ad634253ef',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '48fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '58fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '68fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '78fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '88fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '98fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '41fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
{
id: '42fd662d-ed92-419e-8af9-41afd3fb3d87',
user_id: '5a6fab9c-5849-4be5-973c-5a371165cd57',
article_id: '0aedc83d-5172-4874-bc43-7826e955fccb',
body: 'Description goes here too',
created_at: new Date(),
updated_at: new Date(),
},
],
{},
),
Expand Down
37 changes: 37 additions & 0 deletions src/routes/controllers/users.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moment from 'moment';
import { Op } from 'sequelize';

import models from '../../db/models';
import {
Expand Down Expand Up @@ -268,3 +269,39 @@ export async function getAllUserLikes(req, res) {
return serverError(res);
}
}

/**
*
* @param {Object} req express request
* @param {Object} res express response
* @returns {Array} popular authors
*/
export async function popularAuthors(req, res) {
try {
const authors = await User.findAll({
include: [
{
model: Article,
as: 'publications',
where: { publications: { [Op.not]: [] } },
include: {
model: Comment,
as: 'comments',
where: { comments: { [Op.not]: [] } },
attributes: ['body'],
},
attributes: ['title'],
},
],
limit: 4,
});
return successResponse(
res,
200,
'Successfully retrieved four popular authors',
authors,
);
} catch (error) {
return serverError(res);
}
}
28 changes: 28 additions & 0 deletions src/routes/users.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getUsersBookmarks,
getUsersComments,
getAllUserLikes,
popularAuthors,
} from './controllers/users.controller';

import { checkAuthorizedUser } from './middlewares/authorized-user.middleware';
Expand Down Expand Up @@ -223,4 +224,31 @@ userRouter.get('/comments', checkAuthorizedUser, getUsersComments);
*/
userRouter.get('/likes', checkAuthorizedUser, getAllUserLikes);

/**
* @swagger
*
* /api/v1/users/likes:
* get:
* description: Get popular authors
* produces:
* - application/json
* request:
* content:
* - application/json
* schema:
* type: array
* items:
* $ref: '#/definitions/users'
* responses:
* 200:
* description: Successfully retrieved all popular authors
* 400:
* description: Bad request.
* 404:
* description: No popular authors
* 5XX:
* description: Unexpected error.
*/
userRouter.get('/popular-authors', popularAuthors);

export default userRouter;
2 changes: 1 addition & 1 deletion src/tests/routes/controllers/comment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ describe('CREATE COMMENT', () => {
chai
.request(app)
.post(
'/api/v1/articles/cd75c9de-324e-4b7e-be68-64c0ce09bd4d/comments/18fd662d-ed92-419e-8af9-41afd3fb3d87/thread',
'/api/v1/articles/cd75c9de-324e-4b7e-be68-64c0ce09bd4d/comments/00fd662d-ed92-419e-8af9-41afd3fb3d87/thread',
)
.send({
threadedComment: 'Legit thread comment',
Expand Down
16 changes: 16 additions & 0 deletions src/tests/routes/controllers/users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,20 @@ describe('User stats', () => {
expect(body.data).to.have.property('comments');
});
});

context('Popular authors', () => {
it('returns a 200 and retrieve 4 popular authors', async () => {
const { status, body } = await chai
.request(app)
.get('/api/v1/users/popular-authors')
.set('x-access-token', userToken);

expect(status).to.eql(200);
expect(body).to.have.keys('status', 'message', 'data');
expect(body.message).to.eql(
'Successfully retrieved four popular authors',
);
expect(body.data).to.be.have.length.within(0, 4);
});
});
});

0 comments on commit 5e05944

Please sign in to comment.