Skip to content

Commit

Permalink
feat: req14
Browse files Browse the repository at this point in the history
  • Loading branch information
dopimentel committed Feb 21, 2024
1 parent 1bce261 commit df871f2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ app.post('/categories', auth, categoryController.create);
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.use(error);

Expand Down
12 changes: 12 additions & 0 deletions src/controllers/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,19 @@ const getAll = async (req, res) => {
res.status(200).json(posts);
};

const getById = async (req, res, next) => {
const { id } = req.params;
const post = await postService.getById(id);
if (!post) {
const err = new Error('Post does not exist');
err.status = 404;
return next(err);
}
res.status(200).json(post);
};

module.exports = {
create,
getAll,
getById,
};
12 changes: 12 additions & 0 deletions src/services/postService.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ const getAll = async () => {
return posts;
};

const getById = async (id) => {
const post = await BlogPost.findOne({
where: { id },
include: [
{ model: User, as: 'user', attributes: { exclude: ['password'] } },
{ model: Category, as: 'categories', through: { attributes: [] } },
],
});
return post;
};

module.exports = {
create,
getAll,
getById,
};

0 comments on commit df871f2

Please sign in to comment.