Skip to content

Commit

Permalink
Merge 58584dd into 08e9fb5
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankChinedu committed Aug 21, 2019
2 parents 08e9fb5 + 58584dd commit bf4064e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 32 deletions.
97 changes: 66 additions & 31 deletions controllers/articles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,44 +51,79 @@ export default {

getUserArticles: async (req, res) => {
try {
const { user: { id }, query } = req;
const { user, query, params: { username } } = req;
const offset = query.offset ? (query.offset * query.limit) : 0;
const limit = query.limit || 20;

let userId = null;
if (user) {
userId = user.id;
}
const id = userId;

let usernameId = null;
if (username) {
const foundUser = await db.User.findOne({
where: {
username
}
});
usernameId = foundUser.id;
}

const data = [
{
model: db.Category,
as: 'category',
attributes: ['name']
},
{
model: db.User,
as: 'author',
attributes: ['firstName', 'lastName', ['image', 'authorImage']]
},
{
model: db.ArticleVote,
where: { status: true },
required: false,
as: 'upVote',
attributes: ['status'],
include: [{
model: db.User,
required: false,
as: 'user',
attributes: ['username']
}]
},
{
model: db.ArticleVote,
where: { status: false },
required: false,
as: 'downVote',
attributes: ['status'],
include: [{
model: db.User,
required: false,
as: 'user',
attributes: ['username']
}]
}
];

const include = id ? [...data, {
model: db.Bookmark,
as: 'bookmarks',
where: {
userId: id
},
required: false,
}] : [...data];
const articles = await db.Article.findAndCountAll(
{
offset,
limit,
where: { authorId: id },
attributes: ['title', 'rating', 'reads'],
include: [
{
model: db.ArticleVote,
where: { status: true },
required: false,
as: 'upVote',
attributes: ['status'],
include: [{
model: db.User,
required: false,
as: 'user',
attributes: ['username']
}]
},
{
model: db.ArticleVote,
where: { status: false },
required: false,
as: 'downVote',
attributes: ['status'],
include: [{
model: db.User,
required: false,
as: 'user',
attributes: ['username']
}]
}
]
where: { authorId: (usernameId || id), publish: true },
include
}
);
return res.status(200).json({
Expand Down
2 changes: 1 addition & 1 deletion routes/v1/articles.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ router.post(
);

router.get(
'/user',
'/user/:username?',
Middleware.authenticate,
Middleware.isblackListedToken,
Validation.getUserArticles,
Expand Down

0 comments on commit bf4064e

Please sign in to comment.