-
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.
[feature #167193975] CRUD - category of articles Added
- Loading branch information
1 parent
2c59539
commit c697cea
Showing
17 changed files
with
211 additions
and
15 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,115 @@ | ||
import models from '../../sequelize/models'; | ||
|
||
const { Category, Article } = models; | ||
/** | ||
* @Author - Audace Uhiriwe | ||
*/ | ||
class categoryController { | ||
/** | ||
* Admin - create a new category | ||
* @param {object} req - Request. | ||
* @param {object} res - Response. | ||
* @returns {object} - returns a created article | ||
*/ | ||
static async createCategory(req, res) { | ||
// check if the category exists | ||
const { categoryName } = req.body; | ||
const categoryExists = await Category.findOne({ where: { categoryName } }); | ||
if (categoryExists) { | ||
return res.status(400).send({ error: 'This category is already Exist!' }); | ||
} | ||
const data = await Category.NewCategory(req.body); | ||
return res.status(201).send({ | ||
message: `This category of ${categoryName} has been successfully added!`, | ||
data | ||
}); | ||
} | ||
|
||
/** | ||
* Admin - edit an existing category | ||
* @param {object} req - Request. | ||
* @param {object} res - Response. | ||
* @returns {object} - returns an updated article | ||
*/ | ||
static async editCategory(req, res) { | ||
// check if the category exists | ||
const { categoryName } = req.body; | ||
const { id } = req.params; | ||
const categoryExists = await Category.findAll({ where: { id } }); | ||
if (categoryExists[0].dataValues.categoryName === categoryName) { | ||
return res.status(400).send({ | ||
error: 'This category is already exists, Nothing you changed!' | ||
}); | ||
} | ||
if (categoryExists === null) { | ||
return res.status(404).send({ error: 'This category Not found!' }); | ||
} | ||
const updateCategory = { | ||
categoryName: categoryName || categoryExists[0].dataValues.categoryName | ||
}; | ||
await Category.EditCategory( | ||
updateCategory, | ||
categoryExists[0].dataValues.id | ||
); | ||
return res.status(200).send({ | ||
message: `This category of ${categoryExists[0].dataValues.categoryName} has been successfully updated to ${categoryName}!` | ||
}); | ||
} | ||
|
||
/** | ||
* Admin - delete an existing category | ||
* @param {object} req - Request. | ||
* @param {object} res - Response. | ||
* @returns {object} - returns a message | ||
*/ | ||
static async deleteCategory(req, res) { | ||
const { id } = req.params; | ||
// check if the category exists | ||
const categoryExists = await Category.findOne({ where: { id } }); | ||
if (categoryExists === null) { | ||
return res.status(404).send({ error: 'This category Not found!' }); | ||
} | ||
await Category.destroy({ where: { id } }); | ||
return res.status(200).send({ | ||
message: 'This category has been successfully deleted!' | ||
}); | ||
} | ||
|
||
/** | ||
* Admin - fetch all categories | ||
* @param {object} req - Request. | ||
* @param {object} res - Response. | ||
* @returns {object} - returns an array of all categories | ||
*/ | ||
static async getAllCategory(req, res) { | ||
const categories = await Category.findAll(); | ||
if (categories === null) { | ||
return res.status(200).send({ message: 'No Categories found, so far!' }); | ||
} | ||
return res.status(200).send({ | ||
message: 'Here\'s all categories, so far!', | ||
categories | ||
}); | ||
} | ||
|
||
/** | ||
* Admin - fetch all articles related to a specific category | ||
* @param {object} req - Request. | ||
* @param {object} res - Response. | ||
* @returns {object} - returns an array which contains all articles related to a specific category | ||
*/ | ||
static async fetchAllArticles(req, res) { | ||
const { categoryName } = req.params; | ||
const category = await Category.findOne({ where: { categoryName } }); | ||
if (category === null) { | ||
return res.status(200).send({ message: 'No Articles found with this Category, so far!' }); | ||
} | ||
const articles = await Article.findAll({ where: { categoryName } }); | ||
return res.status(200).send({ | ||
message: 'Here\'s all articles related to that category, so far!', | ||
articles | ||
}); | ||
} | ||
} | ||
|
||
export default categoryController; |
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,15 @@ | ||
import { Router } from 'express'; | ||
import CategoryController from '../controllers/category'; | ||
import Auth from '../../middleware/auth'; | ||
|
||
const categoryRouter = Router(); | ||
const { verifyToken, checkIsAdmin } = Auth; | ||
// eslint-disable-next-line | ||
const { createCategory,editCategory,deleteCategory,getAllCategory,fetchAllArticles } = CategoryController; | ||
|
||
categoryRouter.post('/', verifyToken, checkIsAdmin, createCategory); | ||
categoryRouter.put('/:id', verifyToken, checkIsAdmin, editCategory); | ||
categoryRouter.delete('/:id', verifyToken, checkIsAdmin, deleteCategory); | ||
categoryRouter.get('/', getAllCategory); | ||
categoryRouter.get('/:categoryName/articles', fetchAllArticles); | ||
export default categoryRouter; |
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
26 changes: 26 additions & 0 deletions
26
src/sequelize/migrations/20190710111019-create-category.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,26 @@ | ||
/* eslint-disable no-unused-vars */ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Categories', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
categoryName: { | ||
type: Sequelize.STRING | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Categories') | ||
}; |
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,18 @@ | ||
/* eslint-disable no-unused-vars */ | ||
/* eslint-disable func-names */ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Category = sequelize.define('Category', { | ||
categoryName: DataTypes.STRING, | ||
articleId: DataTypes.INTEGER | ||
}, {}); | ||
|
||
Category.NewCategory = data => Category.create({ categoryName: data.categoryName }); | ||
Category.EditCategory = (data, id) => Category.update( | ||
{ categoryName: data.categoryName }, | ||
{ where: { id } } | ||
); | ||
Category.associate = function (models) { | ||
// associations can be defined here | ||
}; | ||
return Category; | ||
}; |
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
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
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