Skip to content

Commit

Permalink
Merge f41175d into daf7dc0
Browse files Browse the repository at this point in the history
  • Loading branch information
Alpha1202 authored Jul 9, 2019
2 parents daf7dc0 + f41175d commit e66be6f
Show file tree
Hide file tree
Showing 12 changed files with 703 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/controllers/articleController.js
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;
34 changes: 34 additions & 0 deletions src/db/migrations/20190709125502-create-tag.js
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')
};
5 changes: 5 additions & 0 deletions src/db/models/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module.exports = (sequelize, DataTypes) => {
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
Article.hasMany(models.Tag, {
foreignKey: 'articleId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE'
});
};
return Article;
};
9 changes: 9 additions & 0 deletions src/db/models/tag.js
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;
};
77 changes: 77 additions & 0 deletions src/db/seeders/20190701155734-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,83 @@ module.exports = {
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'Funsho',
lastName: 'Funsho',
userName: 'fob412',
email: 'fob412@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'Julius',
lastName: 'Julius',
userName: 'Ju-Ju',
email: 'ju-ju@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'Choko',
lastName: 'naira',
userName: 'Chokonaira',
email: 'chokonaira@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'Femi',
lastName: 'Phembarl',
userName: 'Femi',
email: 'femi2@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'code',
lastName: 'Block',
userName: 'codeblock',
email: 'code@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'Nnamani',
lastName: 'Alpha',
userName: 'Alpha',
email: 'alpha@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
{
firstName: 'nyatti',
lastName: 'team',
userName: 'nyatti',
email: 'team@andela.com',
bio: 'local man is stuck in traffic',
isVerified: true,
password: 'password',
verificationToken: '',
imageUrl: 'image.png'
},
], {}),
down: queryInterface => queryInterface.bulkDelete('Users', null, {})
};
8 changes: 8 additions & 0 deletions src/db/seeders/20190701162733-category.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ module.exports = {
{
name: 'Sports',
slug: 'sports'
},
{
name: 'Culture',
slug: 'culture'
},
{
name: 'Science',
slug: 'science'
}
], {}),
down: queryInterface => queryInterface.bulkDelete('Categories', null, {})
Expand Down
Loading

0 comments on commit e66be6f

Please sign in to comment.