Skip to content

Commit

Permalink
Merge af4e9e9 into d297d0f
Browse files Browse the repository at this point in the history
  • Loading branch information
azupatrick0 committed Dec 17, 2018
2 parents d297d0f + af4e9e9 commit c5361ff
Show file tree
Hide file tree
Showing 12 changed files with 565 additions and 233 deletions.
141 changes: 141 additions & 0 deletions controllers/SearchArticlesController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import model from '../models';
import StatusResponse from '../helpers/StatusResponse';
import pagination from '../helpers/pagination';

const {
articles,
ArticleTag,
} = model;

/**
* @description - This class is all about searching and filtering articles based on parameters
* @param {object} req
* @param {object} res
* @returns {class} Articles searched for
*/
class SearchArticlesController {
/**
* @description - This method takes care of retrieving articles by authors
* @param {object} req
* @param {object} res
* @returns {object} Articles by authors
*/
static async byAuthor(req, res) {
const {
size, page = 1, order = 'DESC', orderBy = 'id'
} = req.query;
try {
const articlesByAuthor = await articles.findAndCountAll({
where: {
userId: req.app.locals.user.userId,
},
order: [[orderBy, order]]
});
const {
limit, offset, totalPages, currentPage
} = pagination(page, size, articlesByAuthor.count);
const fetchedArticles = articlesByAuthor.rows.slice(offset, parseInt(offset, 10)
+ parseInt(limit, 10));

if (fetchedArticles.length >= 1) {
StatusResponse.success(res, {
message: 'All Articles by this author returned succesfully',
articles: fetchedArticles,
metadata: {
count: articlesByAuthor.count,
currentPage,
articleCount: fetchedArticles.length,
limit,
totalPages
}
});
} else {
StatusResponse.notfound(res, {
message: 'No Articles found',
articles: null
});
}
} catch (error) {
StatusResponse.internalServerError(res, {
message: 'An error occured, Articles not returned succesfully, please try again',
error: {
body: [`Internal server error => ${error}`]
}
});
}
}

/**
* @description - This method takes care of retrieving articles by title
* @param {object} req
* @param {object} res
* @returns {object} Articles by title
*/
static async byTitle(req, res) {
try {
const articlesByTitle = await articles.findAndCountAll({
where: {
title: req.query.title,
}
});
if (articlesByTitle.count >= 1) {
StatusResponse.success(res, {
message: 'Articles with this title returned succesfully',
articles: articlesByTitle
});
} else {
StatusResponse.notfound(res, {
message: 'No Articles with such title',
articles: null
});
}
} catch (error) {
StatusResponse.internalServerError(res, {
message: 'An error occured, Articles not returned succesfully, please try again',
error: {
body: [`Internal server error => ${error}`]
}
});
}
}

/**
* @description - This method takes care of retrieving articles by tags
* @param {object} req
* @param {object} res
* @returns {object} Articles by tags
*/
static async byTags(req, res) {
try {
const articlesByTags = await ArticleTag.findAndCountAll({
where: {
tagId: req.app.locals.tag.tagId
},
include: {
model: articles,
},
attributes: { exclude: ['createdAt', 'updatedAt', 'tagId', 'articleId'] }
});
if (articlesByTags.count >= 1) {
StatusResponse.success(res, {
message: 'All Articles using these tags returned succesfully',
articles: articlesByTags
});
} else {
StatusResponse.notfound(res, {
message: 'No Articles with such tags',
articles: null
});
}
} catch (error) {
StatusResponse.internalServerError(res, {
message: 'An error occured, Articles not returned succesfully, please try again',
error: {
body: [`Internal server error => ${error}`]
}
});
}
}
}

export default SearchArticlesController;
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
comment,
bookmarks,
ratings,
likes
likes,
search
} from './routes';

import logger from './config/logger';
Expand All @@ -39,6 +40,7 @@ app.use('/api/v1/users', user);
app.use('/api/v1/articles', articles);
app.use('/api/v1/articles', bookmarks);
app.use('/api/v1/articles', comment);
app.use('/api/v1/articles_search', search);
app.use('/api/v1/ratings', ratings);
app.use('/api/v1/articles', likes);
passportAuth();
Expand Down
27 changes: 24 additions & 3 deletions middlewares/ProfilesMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Response from '../helpers/StatusResponse';
import StatusResponse from '../helpers/StatusResponse';
import model from '../models';

const { profiles } = model;

// This function checks for user id being an integer on [users profile creation]
const checkUsersId = (req, res, next) => {
Expand All @@ -23,7 +26,7 @@ const validProfileInput = (req, res, next) => {
|| !req.body.address
|| !req.body.dateofbirth
) {
Response.badRequest(res, {
StatusResponse.badRequest(res, {
message: 'User input(s) field must me not be empty',
error: {
body: ['Invalid input']
Expand All @@ -33,5 +36,23 @@ const validProfileInput = (req, res, next) => {
return next();
};

// This function checks to see whether a profile exist or not [used in filtering articles by author]
const getAuthor = async (req, res, next) => {
const author = await profiles.findOne({
where: {
username: req.query.author
},
});
if (!author) {
return StatusResponse.notfound(res, {
message: 'No such author',
});
}
req.app.locals.user = {
userId: author.userId,
};
return next();
};

// Export all middlewares here
export { checkUsersId, validProfileInput };
export { checkUsersId, validProfileInput, getAuthor };
22 changes: 21 additions & 1 deletion middlewares/articleMiddleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,24 @@ const checkTags = (req, res, next) => {
return next();
};

export { checkArticle, checkTags };
const { tags } = models;

const getTagId = async (req, res, next) => {
const tag = await tags.findAndCountAll({
where: {
tagName: req.query.tag
},
});
if (tag.count < 1) {
return StatusResponse.notfound(res, {
message: 'No Articles with such tags',
});
}
req.tagId = tag.rows[0].id;
req.app.locals.tag = {
tagId: req.tagId,
};
return next();
};

export { checkArticle, checkTags, getTagId };
Loading

0 comments on commit c5361ff

Please sign in to comment.