-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
386 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Op } from 'sequelize'; | ||
import validator from 'validator'; | ||
import models from '../models'; | ||
import search from '../helpers/search'; | ||
|
||
const { escape } = validator; | ||
const { getIds, searcher } = search; | ||
const { | ||
Articles, Users, Tags, Categories, ArticleTags | ||
} = models; | ||
|
||
const searchController = (req, res) => { | ||
const { | ||
keyword, author, tag, page | ||
} = req.query; | ||
|
||
const currentPage = /^[0-9]+$/.test(page) ? page : 1; | ||
const limit = 10; | ||
const offset = 10 * (currentPage - 1); | ||
const order = [['updatedAt', 'DESC']]; | ||
|
||
const searchOptions = { | ||
limit, offset, order | ||
}; | ||
|
||
// Include details from other models | ||
const include = [ | ||
{ model: Users, attributes: ['id', 'firstname', 'lastname', 'image'] }, | ||
{ model: Categories, as: 'category', attributes: ['name'] } | ||
]; | ||
|
||
if (keyword) { | ||
const queryOptions = { | ||
where: { | ||
[Op.or]: { | ||
title: { [Op.iLike]: `%${escape(keyword)}%` }, | ||
body: { [Op.iLike]: `%${escape(keyword)}%` }, | ||
description: { [Op.iLike]: `%${escape(keyword)}%` } | ||
} | ||
}, | ||
include, | ||
...searchOptions | ||
}; | ||
return searcher(res, Articles, queryOptions, currentPage, 'keyword', escape(keyword)); | ||
} | ||
|
||
if (tag) { | ||
const where = { | ||
name: { | ||
[Op.iLike]: `%${escape(tag)}%` | ||
} | ||
}; | ||
|
||
// returns the list of ids of tags matching the query | ||
return getIds(Tags, where) | ||
.then((tagIds) => { | ||
const queryOptions = { | ||
where: { tagId: { [Op.in]: tagIds } }, | ||
include: [{ | ||
model: Articles, | ||
include | ||
}], | ||
...searchOptions | ||
}; | ||
return searcher(res, ArticleTags, queryOptions, currentPage, 'tag', escape(tag)); | ||
}); | ||
} | ||
|
||
if (author) { | ||
const where = { | ||
[Op.or]: { | ||
username: { [Op.iLike]: `%${escape(author)}%` }, | ||
firstname: { [Op.iLike]: `%${escape(author)}%` }, | ||
lastname: { [Op.iLike]: `%${escape(author)}%` } | ||
} | ||
}; | ||
|
||
// returns the list of ids of authors matching the query | ||
return getIds(Users, where) | ||
.then((authorIds) => { | ||
const queryOptions = { | ||
where: { | ||
userId: { [Op.in]: authorIds } | ||
}, | ||
include, | ||
...searchOptions | ||
}; | ||
return searcher(res, Articles, queryOptions, currentPage, 'author', escape(author)); | ||
}); | ||
} | ||
|
||
return res.status(400).jsend.fail({ | ||
message: 'Request not understood. You can search with either a keyword, author or tag' | ||
}); | ||
}; | ||
|
||
export default searchController; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const search = { | ||
getIds: (model, where) => model.all({ | ||
where, | ||
attributes: ['id'] | ||
}) | ||
.then(results => results.map(result => result.dataValues.id)), | ||
searcher: (res, model, queryOptions, currentPage, type, searchQuery) => { | ||
const { limit } = queryOptions; | ||
|
||
model | ||
.findAndCountAll(queryOptions) | ||
.then((searchResult) => { | ||
const { count } = searchResult; | ||
const totalPages = Math.ceil(count / limit); | ||
|
||
if (searchResult.count < 1) { | ||
return res.status(404).jsend.fail({ | ||
message: `No results for the ${type} "${searchQuery}"` | ||
}); | ||
} | ||
|
||
if (currentPage > totalPages) { | ||
return res.status(404).jsend.fail({ | ||
message: `End of results for the ${type} "${searchQuery}"`, | ||
searchResult, | ||
pageInfo: { currentPage, totalPages } | ||
}); | ||
} | ||
return res.status(200).jsend.success({ | ||
message: `Search results for the ${type} "${searchQuery}"`, | ||
searchResult, | ||
pageInfo: { currentPage, totalPages } | ||
}); | ||
}) | ||
.catch(error => res.status(500).jsend.error(error)); | ||
} | ||
}; | ||
|
||
export default search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('ArticleTags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Articles', | ||
key: 'id' | ||
} | ||
}, | ||
tagId: { | ||
type: Sequelize.INTEGER, | ||
references: { | ||
model: 'Tags', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}, { freezeTableName: true }), | ||
down: queryInterface => queryInterface.dropTable('ArticleTags') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const articleTags = (sequelize, DataTypes) => { | ||
const ArticleTags = sequelize.define('ArticleTags', { | ||
articleId: { | ||
type: DataTypes.INTEGER | ||
}, | ||
tagId: { | ||
type: DataTypes.INTEGER | ||
} | ||
}); | ||
|
||
ArticleTags.associate = (models) => { | ||
ArticleTags.belongsTo(models.Articles, { | ||
foreignKey: 'articleId' | ||
}); | ||
|
||
ArticleTags.belongsTo(models.Tags, { | ||
foreignKey: 'tagId', | ||
}); | ||
}; | ||
|
||
return ArticleTags; | ||
}; | ||
|
||
export default articleTags; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('Tags', [{ | ||
name: 'SomethingFishy', | ||
createdAt: '2018-09-09', | ||
updatedAt: '2018-09-09' | ||
}, { | ||
name: 'CodeForALiving', | ||
createdAt: '2018-09-09', | ||
updatedAt: '2018-09-09' | ||
}, { | ||
name: 'IAmMe', | ||
createdAt: '2018-09-09', | ||
updatedAt: '2018-09-09' | ||
}, { | ||
name: 'Purposefulness', | ||
createdAt: '2018-09-09', | ||
updatedAt: '2018-09-09' | ||
}, { | ||
name: 'GoalGetter', | ||
createdAt: '2018-09-09', | ||
updatedAt: '2018-09-09' | ||
}]), | ||
|
||
down: queryInterface => queryInterface.bulkDelete('Tags') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module.exports = { | ||
up: queryInterface => queryInterface.bulkInsert('ArticleTags', [{ | ||
articleId: 1, | ||
tagId: 1, | ||
createdAt: '2018-09-08', | ||
updatedAt: '2018-09-08' | ||
}, | ||
{ | ||
articleId: 2, | ||
tagId: 2, | ||
createdAt: '2018-09-08', | ||
updatedAt: '2018-09-08' | ||
}, | ||
{ | ||
articleId: 2, | ||
tagId: 1, | ||
createdAt: '2018-09-08', | ||
updatedAt: '2018-09-08' | ||
}]), | ||
down: queryInterface => queryInterface.bulkDelete('ArticleTags') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.