-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from andela/ft-implement-search-functionality-…
…167392449 #167392449 Implement Search Functionality
- Loading branch information
Showing
17 changed files
with
473 additions
and
14 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
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,38 @@ | ||
import BaseRepository from '../repository/base.repository'; | ||
import db from '../database/models'; | ||
import responseGenerator from '../helpers/responseGenerator'; | ||
|
||
/** | ||
* @description class representing Article Controller | ||
* @class SearchController | ||
*/ | ||
class SearchController { | ||
/** | ||
* @description - This method is responsible for searching articles by author | ||
* @static | ||
* @param {object} req - Req sent to the router | ||
* @param {object} res - Response sent from the controller | ||
* @returns {object} - object representing response messages | ||
* @memberof SearchController | ||
*/ | ||
static async generateSearchQuery(req, res) { | ||
try { | ||
let searchResult; | ||
if (req.query.search) { | ||
const { search } = req.query; | ||
const searchModified = search.toLowerCase(); | ||
searchResult = await BaseRepository.searchAll(searchModified); | ||
return responseGenerator.sendSuccess(res, 200, searchResult, null); | ||
} | ||
searchResult = await BaseRepository.findAndCountAll(db.Article, { | ||
attributes: ['id', 'authorId', 'title', 'body', 'image', 'status'] | ||
}); | ||
const { rows } = searchResult; | ||
return responseGenerator.sendSuccess(res, 200, rows, null); | ||
} catch (error) { | ||
return responseGenerator.sendError(res, 500, error.message); | ||
} | ||
} | ||
} | ||
|
||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Tags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
unique: true | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface /* , Sequelize */) => { | ||
return queryInterface.dropTable('Tags'); | ||
} | ||
}; |
40 changes: 40 additions & 0 deletions
40
src/database/migrations/20190722031409-create-article-tags.js
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,40 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('ArticleTags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
references: { | ||
model: 'Articles', | ||
key: 'id' | ||
} | ||
}, | ||
tagId: { | ||
type: Sequelize.INTEGER, | ||
primaryKey: true, | ||
references: { | ||
model: 'Tags', | ||
key: 'id' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
defaultValue: new Date(), | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
|
||
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
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,12 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const ArticleTags = sequelize.define( | ||
'ArticleTags', | ||
{ | ||
articleId: DataTypes.INTEGER, | ||
tagId: DataTypes.INTEGER | ||
}, | ||
{} | ||
); | ||
ArticleTags.associate = () => {}; | ||
return 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,23 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Tags = sequelize.define( | ||
'Tags', | ||
{ | ||
name: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: { | ||
args: true | ||
} | ||
} | ||
}, | ||
{} | ||
); | ||
Tags.associate = models => { | ||
Tags.belongsToMany(models.Article, { | ||
through: 'ArticleTags', | ||
as: 'Articles', | ||
foreignKey: 'tagId' | ||
}); | ||
}; | ||
return 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Router } from 'express'; | ||
import SearchController from '../../controllers/search.controller'; | ||
|
||
const router = Router(); | ||
|
||
router.get('/search', SearchController.generateSearchQuery); | ||
|
||
export default router; |
Oops, something went wrong.