-
Notifications
You must be signed in to change notification settings - Fork 8
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
12 changed files
with
703 additions
and
0 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,89 @@ | ||
import { | ||
Article, User | ||
} from '../db/models'; | ||
|
||
import searchHelper, { searchOutcome } from '../helpers/searchHelper'; | ||
|
||
/** | ||
*@description - Article controller class | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} response | ||
*/ | ||
class ArticleController { | ||
/** | ||
* | ||
*@description search for an article | ||
* @static | ||
* @param {object} req the request body | ||
* @param {object} res the response body | ||
* @returns {object} filtered article object | ||
* @memberof ArticleController | ||
*/ | ||
static async search(req, res) { | ||
const { | ||
author, title: Title, category, tag: tagName | ||
} = req.query; | ||
|
||
try { | ||
if (author) { | ||
const result = await searchHelper.searchByAuthor(author); | ||
const articlesCount = result.length; | ||
|
||
if (result === undefined || result.length === 0) { | ||
return res.status(404).send('The author you selected have no article(s) at the moment, please check your input'); | ||
} | ||
|
||
searchOutcome(result, res, articlesCount); | ||
} else if (Title) { | ||
const result = await searchHelper.searchByTitle(Title); | ||
const articlesCount = result.length; | ||
|
||
if (result === undefined || result.length === 0) { | ||
return res.status(404).send('The title you selected does not exist'); | ||
} | ||
|
||
searchOutcome(result, res, articlesCount); | ||
} else if (category) { | ||
const result = await searchHelper.searchByCategory(category); | ||
const articlesCount = result.length; | ||
|
||
if (result === undefined || result.length === 0) { | ||
return res.status(404).send('The category you selected doesnt exist'); | ||
} | ||
|
||
searchOutcome(result, res, articlesCount); | ||
} else if (tagName) { | ||
const result = await searchHelper.searchByTag(tagName); | ||
const articlesCount = result.length; | ||
|
||
if (result === undefined || result.length === 0) { | ||
return res.status(404).send('The tag you selected does not exist'); | ||
} | ||
|
||
searchOutcome(result, res, articlesCount); | ||
} else { | ||
const result = await Article.findAll({ | ||
include: [{ | ||
model: User, | ||
attributes: { | ||
exclude: ['id', 'password', 'isVerified', 'verificationToken', 'createdAt', 'updatedAt'] | ||
} | ||
}], | ||
limit: 5 | ||
}); | ||
const articlesCount = result.length; | ||
if (result === undefined || result.length === 0) { | ||
return res.status(404).send('There are no articles at the moment'); | ||
} | ||
searchOutcome(result, res, articlesCount); | ||
} | ||
} catch (error) { | ||
return res.status(500).json({ | ||
status: 500, | ||
message: error.message | ||
}); | ||
} | ||
} | ||
} | ||
export default ArticleController; |
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,34 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Tags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
tagName: { | ||
type: Sequelize.STRING | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
required: true, | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'article' | ||
}, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Tag = sequelize.define('Tag', { | ||
tagName: { | ||
type: DataTypes.STRING, | ||
required: true | ||
}, | ||
}, {}); | ||
return Tag; | ||
}; |
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
Oops, something went wrong.