This commit is contained in:
David Arranz 2019-08-20 23:28:48 +02:00
parent ffbbeba404
commit 88f3e5e96f
6 changed files with 1270 additions and 68 deletions

View File

@ -6,26 +6,9 @@ const models = require('../../core/models');
const blogService = require('./blog.service');
const generateCommentService = require('../comments/comment.service');
// Module Name
const MODULE_NAME = '[blog.controller]';
// Error Messages
//const NOT_FOUND = 'Videogame not found';
// Success Messages
//const VG_CT_VIDEOGAME_DELETED_SUCCESSFULLY = 'Videogame deleted successfully';
//const blogCommentsService = generateCommentService(models.Post.Comments);
const controllerOptions = {
MODULE_NAME,
params: {
find: { includeAll: true },
findOne: { includeAll: true, paginate: { limit: 1, page: 1 } },
count: {},
}
};
const controllerOptions = { MODULE_NAME };
const extraControllers = {
getPostComments: async (req, res, next) => {

View File

@ -16,9 +16,10 @@ routes.get('/posts', isLoggedUser,
}),
PaginateMiddleware.middleware(),
SortMiddleware.middleware({ default: "date" }),
blogController.find());
routes.post('/posts', isAdministratorUser, blogController.create);
blogController.find({
scopes: ['defaultScope'],
})
);
routes.get('/posts/:id', isLoggedUser,
FieldMiddleware.middleware({
@ -29,14 +30,27 @@ routes.get('/posts/:id', isLoggedUser,
blogController.findOne()
);
routes.put('/posts/:id', isAdministratorUser, blogController.update);
routes.delete('/posts/:id', isAdministratorUser, blogController.delete);
routes.get('/posts/:id/comments', isLoggedUser,
PaginateMiddleware.middleware(),
SortMiddleware.middleware({ default: "date" }),
blogController.getPostComments);
/********************************************************************************************************
* ADMINISTRACIÓN
*********************************************************************************************************
*/
// Todos los posts
routes.get('/admin/posts',
isAdministratorUser,
SortMiddleware.middleware({ default: "-date" }),
blogController.find({
scopes: ['includeAllStates'],
})
);
routes.post('/admin/posts', isAdministratorUser, blogController.create);
routes.put('/admin/posts/:id', isAdministratorUser, blogController.update);
routes.delete('/admin/posts/:id', isAdministratorUser, blogController.delete);
module.exports = routes;

View File

@ -26,6 +26,7 @@ module.exports = function (sequelize, DataTypes) {
Category.associate = function (models) {
Category.Posts = Category.belongsToMany(models.Post, {
as: 'posts',
through: models.PostCategory,
foreignKey: 'categoryId'
});

View File

@ -11,7 +11,7 @@ module.exports = function (sequelize, DataTypes) {
foreignKey: true
}
}, {
tableName: 'post_category',
tableName: 'posts_categories',
freezeTableName: true,
timestamps: false
});

View File

@ -24,19 +24,35 @@ module.exports = function (sequelize, DataTypes) {
type: DataTypes.TEXT,
allowNull: false
},
link: {
type: DataTypes.STRING,
allowNull: false
},
state: {
type: DataTypes.STRING,
allowNull: false,
defaultValue: 'draft'
},
}, {
tableName: 'post',
tableName: 'posts',
freezeTableName: true,
timestamps: true,
defaultScope: {
where: {
state: 'publish',
},
},
});
Post.associate = function (models) {
Post.Categories = Post.belongsToMany(models.Category, {
as: 'categories',
through: models.PostCategory,
foreignKey: 'postId'
});
//Post.Reactions = Post.hasMany(models.PostReaction, { foreignKey: 'postId' });
//Post.User = Post.belongsTo(models.User, { foreignKey: 'userId' });
//OJO antes de force comentar
// OJO GENERA UN FOREIGN KEY Con eventos y habrá ID de otras entidades que no exitan en la tabla eventos, porque son post o speakers
@ -51,5 +67,29 @@ module.exports = function (sequelize, DataTypes) {
});
};
Post.addScope('includeAllStates', () => {
return {
where: {
state: ['publish', 'draft']
},
}
});
Post.addScope('includeMultimedias', () => {
return {
include: [{
model: sequelize.models.Multimedia,
as: { singular: 'multimedia', plural: 'multimedias' },
required: false,
include: [{
model: sequelize.models.MultimediaFile,
as: "multimediaFile"
}]
},
]
}
});
return Post;
};

1244
yarn.lock

File diff suppressed because it is too large Load Diff