Skip to content

Commit

Permalink
Merge 667002c into a9a516a
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueayo1988 committed Mar 14, 2019
2 parents a9a516a + 667002c commit d7eb337
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
72 changes: 65 additions & 7 deletions server/controllers/articleSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import db from '../database/models';
import { response } from '../utils';

const { Op } = Sequelize;
const { Article, User } = db;
const { Article, User, Tag } = db;
dotenv.config();

/**
Expand All @@ -30,9 +30,11 @@ export default class ArticleSearch {
static async search(req, res) {
const titleResults = ArticleSearch.searchByTitle(req, res);
const authorResults = ArticleSearch.filterByAuthors(req, res);
const tagResults = ArticleSearch.searchByTag(req, res);

const allResults = await Promise.all([titleResults, authorResults]);
const allResults = await Promise.all([titleResults, authorResults, tagResults]);
const searchResults = allResults.some(result => result.length > 0);

if (searchResults) {
return response(res).success({ allResults });
}
Expand Down Expand Up @@ -60,6 +62,38 @@ export default class ArticleSearch {
}
}

/**
* @description searchByTag method
* @param {*} req
* @param {*} res
* @returns {Array} articles
*/
static async searchByTag(req, res) {
try {
const keyword = ArticleSearch.modifyString(req.query.title);
const tag = await Tag.findAll({
where: { name: { [Op.iLike]: `%${keyword}%` } },
raw: true
});

if (tag.length === 0) {
return [];
}

const tagId = (tag[0].id);

const articles = await Article.findAll({
raw: true,
where: { tagId },
include: [{ model: User, attributes: ['username', 'bio', 'image'] }]
});

return articles;
} catch (err) {
return response(res).serverError({ errors: { server: ['database error'] } });
}
}

/**
* @description filter method
* @param {*} req
Expand All @@ -69,15 +103,39 @@ export default class ArticleSearch {
static async filter(req, res) {
try {
const title = ArticleSearch.modifyString(req.query.title);
const author = ArticleSearch.modifyString(req.query.author);
const tag = ArticleSearch.modifyString(req.query.tag);

const user = await User.findAll({
where: { username: author },
raw: true
});

const userId = user.length === 0 ? 0 : user[0].id;

const tagResult = await Tag.findAll({
where: { name: tag },
raw: true
});

const tagId = tagResult.length === 0 ? 0 : tagResult[0].id;

const articles = await Article.findAll({
where: { title: { [Op.iLike]: `%${title}` } },
raw: true,
attributes: { exclude: ['createdAt', 'updatedAt'] }
where: {
[Op.or]: [
{ title },
{ tagId },
{ userId }
]
},
raw: true
});

if (!articles[0]) response(res).notFound({ message: `no article found with title '${title}'` });
else response(res).success({ articles });
if (articles.length === 0) {
return response(res).notFound({ message: 'No article found with that match' });
}

response(res).success({ articles });
} catch (err) {
return response(res).serverError();
}
Expand Down
20 changes: 16 additions & 4 deletions server/test/articleSearch.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,34 @@ describe('test filter', () => {
it('should return 200 error and an article', (done) => {
chai
.request(app)
.get('/api/filter?title=This is an article')
.get('/api/filter?title=This is an article&tag=&author=')
.end((err, res) => {
expect(res).to.have.status(200);
expect(res.body.articles).to.be.an('array');
done();
});
});

it('should return 404 error when title is not found', (done) => {
it('should return 404 error when article is not found', (done) => {
chai
.request(app)
.get('/api/filter?title=bland morning')
.get('/api/filter?title=bland morning&tag=tech&author=nameless')
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('no article found with title \'bland morning\'');
expect(res.body.message).to.equal('No article found with that match');
done();
});
});

it('should return 404 error when there is no parameter for tag', (done) => {
chai
.request(app)
.get('/api/filter?title=bland morning&tag=&author=nameless')
.end((err, res) => {
expect(res).to.have.status(404);
expect(res.body.message).to.be.a('string');
expect(res.body.message).to.equal('No article found with that match');
done();
});
});
Expand Down

0 comments on commit d7eb337

Please sign in to comment.