diff --git a/src/app.js b/src/app.js index deb67f5..5be9d1c 100644 --- a/src/app.js +++ b/src/app.js @@ -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(); @@ -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); diff --git a/src/controllers/index.js b/src/controllers/index.js index 4571f59..e690566 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -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, }; \ No newline at end of file diff --git a/src/migrations/20230914144754-create-blog-posts.js b/src/migrations/20230914144754-create-blog-posts.js index 662af70..62b75ba 100644 --- a/src/migrations/20230914144754-create-blog-posts.js +++ b/src/migrations/20230914144754-create-blog-posts.js @@ -13,7 +13,7 @@ module.exports = { content: { allowNull: false, type: Sequelize.STRING, - unique: true, + // unique: true, }, userId: { allowNull: false, diff --git a/src/models/BlogPost.js b/src/models/BlogPost.js index e3f4cbd..57aa1ad 100644 --- a/src/models/BlogPost.js +++ b/src/models/BlogPost.js @@ -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'), }, }, diff --git a/src/services/postService.js b/src/services/postService.js index e69de29..18812a6 100644 --- a/src/services/postService.js +++ b/src/services/postService.js @@ -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, +};