Skip to content

Commit

Permalink
req12 - parcial
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent 784cece commit 3cbc0ba
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const express = require('express');
const { loginController, userController, categoryController } = require('./controllers');
const { loginController, userController, categoryController, postController } = require('./controllers');
const { error, auth } = require('./middlewares');

const app = express();
Expand All @@ -16,10 +16,7 @@ app.get('/user', auth, userController.getAll);
app.get('/user/:id', auth, userController.getById);
app.post('/categories', auth, categoryController.create);
app.get('/categories', auth, categoryController.getAll);
app.post('/post', auth, async (req, res) => {
const { title, content, categoryIds, userId } = req.body;
res.status(200).json({ title, content, categoryIds, userId });
});
app.post('/post', auth, postController.create);


app.use(error);
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const userController = require('./userController');
const loginController = require('./loginController');
const categoryController = require('./categoryController');
const postController = require('./postController');

module.exports = {
userController,
loginController,
categoryController,
postController,
};
2 changes: 1 addition & 1 deletion src/migrations/20230914144754-create-blog-posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
content: {
allowNull: false,
type: Sequelize.STRING,
unique: true,
// unique: true,
},
userId: {
allowNull: false,
Expand Down
4 changes: 4 additions & 0 deletions src/models/BlogPost.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@ module.exports = (sequelize, DataTypes) => {
allowNull: false,
type: DataTypes.DATE,
field: 'published',
defaultValue: sequelize.fn('now'),
// defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
},
updated: {
allowNull: false,
type: DataTypes.DATE,
field: 'updated',
defaultValue: sequelize.fn('now'),
// defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
},
},

Expand Down
23 changes: 23 additions & 0 deletions src/services/postService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { BlogPost, Category } = require('../models');

const getById = async (id) => {
const post = await BlogPost.findOne({
where: { id }
});
return post;
};

const create = async ({ title, content, categoryIds, userId }) => {
console.log(userId);

const { dataValues } = await BlogPost.create({ title, content, userId });
// await post.addCategories(categoryIds);
console.log(dataValues.id);
const post = await getById(dataValues.id);
console.log(post);
return post;
};

module.exports = {
create,
};

0 comments on commit 3cbc0ba

Please sign in to comment.