-
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.
167371017-feature(category, article): add category
- add category migration and model - add category route - add category validation - alter article table add categoryId column - add tests [Delivers #167371017]
- Loading branch information
Obi chinedu Frank
committed
Jul 22, 2019
1 parent
ab6ccbe
commit 0f01a09
Showing
13 changed files
with
284 additions
and
4 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,28 @@ | ||
import db from '../../db/models'; | ||
|
||
export default { | ||
addCategory: async (req, res) => { | ||
const { name } = req.body; | ||
|
||
try { | ||
await db.Category.create({ | ||
name, | ||
}); | ||
|
||
return res.status(200).send({ | ||
message: 'category added successfully' | ||
}); | ||
} catch (e) { | ||
return res.status(400).send({ | ||
error: e.message, | ||
}); | ||
} | ||
}, | ||
|
||
getCategories: async (req, res) => { | ||
const categories = await db.Category.findAll(); | ||
return res.status(200).send({ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Categories', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING, | ||
unique: true | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: queryInterface => queryInterface.dropTable('Categories') | ||
}; |
8 changes: 8 additions & 0 deletions
8
db/migrations/20190721220659-add_category_id_column_to_articles_table.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,8 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.addColumn('Articles', 'categoryId', { | ||
type: Sequelize.INTEGER, | ||
allowNull: true, | ||
defaultValue: null, | ||
}), | ||
down: queryInterface => queryInterface.removeColumn('Articles', 'categoryId') | ||
}; |
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,12 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Category = sequelize.define('Category', { | ||
name: DataTypes.STRING | ||
}, {}); | ||
Category.associate = (models) => { | ||
Category.hasMany(models.Article, { | ||
foreignKey: 'categoryId', | ||
as: 'articles', | ||
}); | ||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import express from 'express'; | ||
import Category from '../../controllers/categories'; | ||
import Middleware from '../../middlewares'; | ||
import Validation from '../../validators/category'; | ||
|
||
const router = express.Router(); | ||
|
||
router.post( | ||
'/', | ||
Middleware.authenticate, | ||
Middleware.isblackListedToken, | ||
Middleware.isAdmin, | ||
Validation.category, | ||
Category.addCategory | ||
); | ||
|
||
router.get( | ||
'/', | ||
Middleware.authenticate, | ||
Middleware.isblackListedToken, | ||
Category.getCategories | ||
); | ||
|
||
export default router; |
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 |
---|---|---|
|
@@ -732,6 +732,7 @@ definitions: | |
- description | ||
- body | ||
- images | ||
- categoryId | ||
properties: | ||
title: | ||
type: string | ||
|
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.