Skip to content

Commit

Permalink
update post by id
Browse files Browse the repository at this point in the history
  • Loading branch information
taugk committed Dec 12, 2023
1 parent b84b869 commit cc3332d
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions routes/postsRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,31 @@ router.get("/latestPosts", async (req, res) => {

router.get("/postsById/:id", async (req, res) => {
try {
const post = await Post.findByPk(req.params.id);
const post = await Post.findByPk(req.params.id, {
include: [
{
model: User,
attributes: ["id", "fullname", "email", "no_hp", "photo_url"],
},
],
});

if (!post) {
return res.status(404).json({ message: "Post not found" });
}

const unixPickupTime = moment(post.pickupTime).unix();
const unixCreatedAt = moment(post.createdAt).unix();
const unixUpdatedAt = moment(post.updatedAt).unix();

const responsePost = {
...post.toJSON(),
pickupTime: unixPickupTime,
createdAt: unixCreatedAt,
updatedAt: unixUpdatedAt,
};

const user = await User.findByPk(post.userId);
if (!user) {
return res.status(401).send("User not found");
}

const userInfo = {
id: user.id,
full_name: user.fullname,
email: user.email,
no_hp: user.no_hp,
photo_url: user.photo_url,
};

const combine = {
post: responsePost,
userInfo: userInfo,
};

res.status(200).json(combine);
res.status(200).json(responsePost);
} catch (error) {
console.error("Error fetching post", error);
res.status(500).json({ message: "Internal server error" });
Expand Down

0 comments on commit cc3332d

Please sign in to comment.