Skip to content

Commit

Permalink
Merge 91a61f5 into cd1ab34
Browse files Browse the repository at this point in the history
  • Loading branch information
walsamlee committed Apr 15, 2019
2 parents cd1ab34 + 91a61f5 commit 6601b4a
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 136 deletions.
2 changes: 2 additions & 0 deletions server/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import getFollowersController from './get-followers.controllers';
import getDailyStatistics from './statistics.controller';
import adminController from './admin.controllers';
import bookmarkController from './bookmark.controller';
import searchArticles from './search-article.controllers';

export default {
profileController,
Expand All @@ -24,4 +25,5 @@ export default {
getDailyStatistics,
adminController,
bookmarkController,
searchArticles,
};
106 changes: 106 additions & 0 deletions server/controllers/search-article.controllers.js
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;
113 changes: 62 additions & 51 deletions server/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,58 +49,69 @@
*/

module.exports = (sequelize, DataTypes) => {
const Article = sequelize.define('Article', {
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
const Article = sequelize.define(
'Article',
{
id: {
allowNull: false,
primaryKey: true,
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
unique: true,
},
user_id: {
type: DataTypes.UUID,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
slug: {
type: DataTypes.STRING,
unique: true,
},
abstract: {
type: DataTypes.STRING,
},
body: {
type: DataTypes.STRING,
},
category: {
type: DataTypes.STRING,
},
image_url: {
type: DataTypes.STRING,
},
bookmark_count: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
likes_count: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
is_reported: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
is_draft: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
reading_time: {
type: DataTypes.INTEGER,
},
},
user_id: {
type: DataTypes.UUID,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: false,
},
slug: {
type: DataTypes.STRING,
unique: true,
},
abstract: {
type: DataTypes.STRING,
},
body: {
type: DataTypes.STRING,
},
category: {
type: DataTypes.STRING,
},
image_url: {
type: DataTypes.STRING,
},
bookmark_count: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
likes_count: {
type: DataTypes.INTEGER,
defaultValue: 0,
},
is_reported: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
is_draft: {
type: DataTypes.BOOLEAN,
defaultValue: false,
},
reading_time: {
type: DataTypes.INTEGER,
},
});
{
indexes: [
{
unique: true,
fields: ['title'],
},
],
}
);
Article.associate = models =>
Article.belongsTo(models.User, {
foreignKey: 'user_id',
Expand Down
44 changes: 28 additions & 16 deletions server/models/keyword.js
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;
};
Loading

0 comments on commit 6601b4a

Please sign in to comment.