-
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
8 changed files
with
350 additions
and
136 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,106 @@ | ||
import sequelize from 'sequelize'; | ||
import models from '../models'; | ||
import serverError from '../helpers/server-error'; | ||
|
||
const { Article, User, Keyword } = models; | ||
|
||
const searchArticles = async (req, res) => { | ||
try { | ||
if (!Object.keys(req.query).length) { | ||
return res.status(400).json({ | ||
errors: { | ||
body: ['No search query provided'], | ||
}, | ||
}); | ||
} | ||
const searchFilter = req.query.search; | ||
const articles = Article.findAll({ | ||
where: { | ||
title: { | ||
[sequelize.Op.iLike]: `%${searchFilter}%`, | ||
}, | ||
}, | ||
attributes: [ | ||
'slug', | ||
'title', | ||
'body', | ||
'createdAt', | ||
'updatedAt', | ||
'likes_count', | ||
], | ||
include: [ | ||
{ | ||
model: User, | ||
as: 'author', | ||
attributes: ['first_name', 'last_name'], | ||
}, | ||
], | ||
}); | ||
|
||
const keywords = Keyword.findAll({ | ||
where: { | ||
keyword: { | ||
[sequelize.Op.iLike]: `%${searchFilter}%`, | ||
}, | ||
}, | ||
attributes: ['keyword'], | ||
include: [ | ||
{ | ||
model: Article, | ||
as: 'article', | ||
attributes: [ | ||
'slug', | ||
'title', | ||
'body', | ||
'createdAt', | ||
'updatedAt', | ||
'likes_count', | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
const authors = User.findAll({ | ||
where: { | ||
first_name: { | ||
[sequelize.Op.iLike]: `%${searchFilter}%`, | ||
}, | ||
}, | ||
attributes: ['first_name', 'last_name'], | ||
include: [ | ||
{ | ||
model: Article, | ||
as: 'author', | ||
attributes: [ | ||
'slug', | ||
'title', | ||
'body', | ||
'createdAt', | ||
'updatedAt', | ||
'likes_count', | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
const [articleFilter, keywordFilter, authorFilter] = await Promise.all([ | ||
articles, | ||
keywords, | ||
authors, | ||
]); | ||
|
||
return res.status(200).json({ | ||
articleFilter, | ||
keywordFilter, | ||
authorFilter, | ||
}); | ||
} catch (e) { | ||
return res.status(500).json({ | ||
errors: serverError(), | ||
}); | ||
} | ||
}; | ||
|
||
const Articles = { searchArticles }; | ||
|
||
export default Articles; |
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 |
---|---|---|
@@ -1,24 +1,36 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Keyword = sequelize.define('Keyword', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
unique: true, | ||
const Keyword = sequelize.define( | ||
'Keyword', | ||
{ | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
unique: true, | ||
}, | ||
article_id: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
}, | ||
keyword: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}, | ||
article_id: { | ||
type: DataTypes.UUID, | ||
allowNull: false, | ||
}, | ||
keyword: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
}); | ||
{ | ||
indexes: [ | ||
{ | ||
unique: true, | ||
fields: ['keyword'], | ||
}, | ||
], | ||
} | ||
); | ||
Keyword.associate = models => | ||
Keyword.belongsTo(models.Article, { | ||
foreignKey: 'article_id', | ||
as: 'article', | ||
}); | ||
return Keyword; | ||
}; |
Oops, something went wrong.