Skip to content

Commit

Permalink
feat: req15
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent df871f2 commit 465c16b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const express = require('express');
const {
loginController, userController, categoryController, postController } = require('./controllers');
const { error, auth } = require('./middlewares');

// const { postService } = require('./services');
const app = express();

app.get('/', (_request, response) => {
Expand All @@ -20,6 +20,13 @@ app.get('/categories', auth, categoryController.getAll);
app.post('/post', auth, postController.create);
app.get('/post', auth, postController.getAll);
app.get('/post/:id', auth, postController.getById);
app.put('/post/:id', auth, postController.update);
// async (req, res) => {
// const { id } = req.params;
// const { title, content } = req.body;
// const post = await postService.update({ id, title, content });
// res.status(200).json(post);
// });

app.use(error);

Expand Down
17 changes: 16 additions & 1 deletion src/controllers/postController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { postService } = require('../services');
const { postService, updatePostService } = require('../services');

const create = async (req, res, next) => {
const { title, content, categoryIds, userId } = req.body;
Expand All @@ -23,8 +23,23 @@ const getById = async (req, res, next) => {
res.status(200).json(post);
};

const update = async (req, res, next) => {
const { id } = req.params;
const { title, content, userId } = req.body;
const response = await updatePostService.update({ id, title, content, userId });
if (!response) {
const err = new Error('Post does not exist');
err.status = 404;
return next(err);
}
if (response.error) return next(response.error);

res.status(200).json(response);
};

module.exports = {
create,
getAll,
getById,
update,
};
2 changes: 2 additions & 0 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const userService = require('./userService');
const loginService = require('./loginService');
const categoryService = require('./categoryService');
const postService = require('./postService');
const updatePostService = require('./update.postService');

module.exports = {
userService,
loginService,
categoryService,
postService,
updatePostService,
};
2 changes: 1 addition & 1 deletion src/services/postService.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const getById = async (id) => {
});
return post;
};

module.exports = {
create,
getAll,
Expand Down
2 changes: 2 additions & 0 deletions src/services/validations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const loginValidation = require('./loginValidation');
const createUserValidation = require('./createUserValidation');
const createCategoryValidation = require('./createCategoryValidation');
const createPostValidation = require('./createPostValidation');
const updatePostValidation = require('./updatePostValidation');

module.exports = {
loginValidation,
createUserValidation,
createCategoryValidation,
createPostValidation,
updatePostValidation,
};

0 comments on commit 465c16b

Please sign in to comment.