-
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(novels): added create novel endpoint
added verifyToken middleware [Starts #167164986]
- Loading branch information
Showing
23 changed files
with
521 additions
and
14 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
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 @@ | ||
import createNewNovel from '../services/novel'; | ||
|
||
/** | ||
* createNovel | ||
* | ||
* @param {object} req | ||
* @param {object} res | ||
* @returns {object} json | ||
*/ | ||
const createNovel = async (req, res) => { | ||
const newNovel = req.body; | ||
const createdNovel = await createNewNovel(newNovel, req.userId); | ||
if (createdNovel.error) { | ||
return res.status(409).json({ | ||
...createdNovel | ||
}); | ||
} | ||
return res.status(201).json({ | ||
message: 'Novel created successfully', | ||
novel: { | ||
...createdNovel | ||
} | ||
}); | ||
}; | ||
|
||
export default createNovel; |
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 @@ | ||
const up = (queryInterface, Sequelize) => queryInterface.createTable('Genres', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
name: { | ||
type: Sequelize.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
|
||
const down = queryInterface => queryInterface.dropTable('Genres'); | ||
|
||
export default { up, down }; |
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,59 @@ | ||
const up = (queryInterface, Sequelize) => queryInterface.createTable('Novels', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
authorId: { | ||
type: Sequelize.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'authorId', | ||
}, | ||
}, | ||
genreId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: 'Genres', | ||
key: 'id', | ||
as: 'genreId', | ||
}, | ||
}, | ||
slug: { | ||
type: Sequelize.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
title: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
description: { | ||
allowNull: false, | ||
type: Sequelize.STRING | ||
}, | ||
body: { | ||
allowNull: false, | ||
type: Sequelize.TEXT | ||
}, | ||
favoritesCount: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
|
||
const down = queryInterface => queryInterface.dropTable('Novels'); | ||
|
||
export default { up, down }; |
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,29 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Genre = sequelize.define('Genre', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER | ||
}, | ||
name: { | ||
type: DataTypes.STRING | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: DataTypes.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: DataTypes.DATE | ||
} | ||
}, {}); | ||
Genre.associate = (models) => { | ||
// associations can be defined here | ||
Genre.hasMany(models.Novel, { | ||
foreignKey: 'genreId', | ||
as: 'genre', | ||
}); | ||
}; | ||
return Genre; | ||
}; |
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,58 @@ | ||
export default (sequelize, DataTypes) => { | ||
const Novel = sequelize.define('Novel', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: DataTypes.INTEGER | ||
}, | ||
authorId: { | ||
type: DataTypes.INTEGER, | ||
onDelete: 'CASCADE', | ||
allowNull: false | ||
}, | ||
genreId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
slug: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
unique: true | ||
}, | ||
title: { | ||
allowNull: false, | ||
type: DataTypes.STRING | ||
}, | ||
description: { | ||
allowNull: false, | ||
type: DataTypes.STRING | ||
}, | ||
body: { | ||
allowNull: false, | ||
type: DataTypes.TEXT | ||
}, | ||
favoritesCount: { | ||
type: DataTypes.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: DataTypes.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: DataTypes.DATE | ||
} | ||
}, {}); | ||
Novel.associate = (models) => { | ||
// associations can be defined here | ||
Novel.belongsTo(models.User, { | ||
foreignKey: 'authorId', | ||
onDelete: 'CASCADE', | ||
}); | ||
Novel.belongsTo(models.Genre, { | ||
foreignKey: 'genreId' | ||
}); | ||
}; | ||
return Novel; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
export const up = queryInterface => queryInterface.bulkInsert('Genres', [{ | ||
name: 'action', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
name: 'thriller', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
name: 'romance', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
name: 'fiction', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
name: 'motivational', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
], {}); | ||
|
||
export const down = queryInterface => queryInterface.bulkDelete('Genres', null, {}); |
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.