Skip to content

Commit

Permalink
feat(search): change word to title
Browse files Browse the repository at this point in the history
  • Loading branch information
tejiri4 committed Jan 29, 2019
1 parent 0751ec0 commit fd1681b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 38 deletions.
10 changes: 5 additions & 5 deletions server/controllers/ArticleController.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,13 @@ class ArticleController {
* @returns {object} api route response
*/
static async search(req, res) {
const { author, tag, word } = req.query;
const { author, tag, title } = req.query;
if (author) {
SearchController.byAuthor(author, res);
SearchController.byAuthor(author, req, res);
} else if (tag) {
SearchController.byTags(tag, res);
} else if (word) {
SearchController.byWord(word, res);
SearchController.byTags(tag, req, res);
} else if (title) {
SearchController.byTitle(title, req, res);
} else {
return response(
res,
Expand Down
96 changes: 65 additions & 31 deletions server/controllers/SearchController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Sequelize from 'sequelize';
import response from '../helpers/response';
import db from '../models';

import pagination from '../helpers/pagination';

const {
User, Tag, Article
Expand All @@ -15,12 +15,17 @@ class SearchController {
* @static
* @description a function that handles searching articles by author
* @param {String} author
* @param {object} req HTTP request object
* @param {object} res HTTP response object
* @returns {object} api route response with created article info
*/
static async byAuthor(author, res) {
static async byAuthor(author, req, res) {
try {
const findAuthor = await User.findAll({
const { query } = req;
const limit = Number(query.limit) || 20;
const currentPage = Number(query.page) || 1;
const offset = (currentPage - 1) * limit;
const findAuthor = await User.findAndCountAll({
where: {
[Op.or]: [
{
Expand All @@ -35,10 +40,18 @@ class SearchController {
}
]
},
attributes: ['id', 'userName', 'fullName', 'bio', 'img']
attributes: ['id', 'userName', 'fullName', 'bio', 'img'],
limit,
offset
});
if (findAuthor.length > 0) {
return response(res, 200, 'success', 'Author found', null, findAuthor);
const totalAuthor = findAuthor.count;
const paginatedData = pagination(findAuthor.rows.length, limit, currentPage, totalAuthor);
const data = {
authors: findAuthor,
paginatedData
};
if (totalAuthor > 0) {
return response(res, 200, 'success', 'Author found', null, data);
}
return response(res, 404, 'failure', 'Author not found', null, null);
} catch (error) {
Expand All @@ -50,12 +63,17 @@ class SearchController {
* @static
* @description a function that handles searching articles by author
* @param {String} tag
* @param {object} req HTTP request object
* @param {object} res HTTP response object
* @returns {object} api route response with created article info
*/
static async byTags(tag, res) {
static async byTags(tag, req, res) {
try {
const findTag = await Tag.findAll({
const { query } = req;
const limit = Number(query.limit) || 20;
const currentPage = Number(query.page) || 1;
const offset = (currentPage - 1) * limit;
const findTag = await Tag.findAndCountAll({
where: {
name: {
[Op.iLike]: `%${tag}%`
Expand All @@ -70,11 +88,18 @@ class SearchController {
through: {
attributes: []
}
}]
}],
limit,
offset
});

if (findTag.length > 0) {
return response(res, 200, 'success', 'Tag found', null, findTag);
const totalTag = findTag.count;
const paginatedData = pagination(findTag.rows.length, limit, currentPage, totalTag);
const data = {
tags: findTag,
paginatedData
};
if (totalTag > 0) {
return response(res, 200, 'success', 'Tag found', null, data);
}
return response(res, 404, 'failure', 'Tag not found', null, null);
} catch (error) {
Expand All @@ -85,34 +110,44 @@ class SearchController {
/**
* @static
* @description a function that handles searching articles by author
* @param {String} word
* @param {String} title
* @param {object} req HTTP request object
* @param {object} res HTTP response object
* @returns {object} api route response with created article info
*/
static async byWord(word, res) {
static async byTitle(title, req, res) {
try {
const findArticles = await Article.findAll({
const { query } = req;
const limit = Number(query.limit) || 20;
const currentPage = Number(query.page) || 1;
const offset = (currentPage - 1) * limit;
const findArticles = await Article.findAndCountAll({
where: {
title: {
[Op.iLike]: `%${word}%`
[Op.iLike]: `%${title}%`
}
},
attributes: { exclude: ['userId'] },
include: [{
model: User,
as: 'author',
attributes: ['id', 'userName', 'bio', 'img'],
},
{
model: Tag,
as: 'tags',
attributes: ['name'],
through: { attributes: [] }
},
]
include: [
{
model: Tag,
as: 'tags',
attributes: ['name'],
through: { attributes: [] }
}
],
offset,
limit
});
if (findArticles.length > 0) {
return response(res, 200, 'success', 'Article found', null, findArticles);

const totalArticle = findArticles.count;
const paginatedData = pagination(findArticles.rows.length, limit, currentPage, totalArticle);
const data = {
articles: findArticles,
paginatedData
};
if (totalArticle > 0) {
return response(res, 200, 'success', 'Article found', null, data);
}
return response(res, 404, 'failure', 'Article not found', null, null);
} catch (error) {
Expand All @@ -121,5 +156,4 @@ class SearchController {
}
}


export default SearchController;
4 changes: 2 additions & 2 deletions server/test/controllers/searchController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Search Model', () => {
it('It should be to search articles by title or content', async () => {
const response = await chai
.request(app)
.get('/api/v1/search?word=to')
.get('/api/v1/search?title=to')
.set('Authorization', `Bearer ${token}`)
expect(response.status).to.eqls(200);
expect(response.body.status).to.eqls('success');
Expand All @@ -57,7 +57,7 @@ describe('Search Model', () => {
it('It should not be able to search for articles if inputed value dont exits ', async () => {
const response = await chai
.request(app)
.get('/api/v1/search?word=*')
.get('/api/v1/search?title=*')
.set('Authorization', `Bearer ${token}`)
expect(response.status).to.eqls(404);
expect(response.body.status).to.eqls('failure');
Expand Down

0 comments on commit fd1681b

Please sign in to comment.