Skip to content

Commit 825522c

Browse files
Merge pull request #46 from AlgoAce-Shrao/fix/endpoints-fix
Fixed the endpoint for untracked modules
2 parents d077d4e + 0e42d1a commit 825522c

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

backend/src/controllers/postController.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,43 @@ export const createPost = async (req: Request & { userId?: string }, res: Respon
3131
return res.status(500).json({ message: "Internal server error" });
3232
}
3333
};
34+
35+
export const updatePost = async (
36+
req: Request & { userId?: string },
37+
res: Response
38+
) => {
39+
try {
40+
if (!req.userId) {
41+
return res.status(401).json({ message: "Unauthorized" });
42+
}
43+
44+
const { postId } = req.params;
45+
const { title, description, tags = [], media = [] } =
46+
req.body as CreatePostInput;
47+
48+
const post = await Post.findById(postId);
49+
if (!post) {
50+
return res.status(404).json({ message: "Post not found" });
51+
}
52+
53+
if (post.userId.toString() !== req.userId) {
54+
return res.status(403).json({ message: "Forbidden" });
55+
}
56+
57+
const updateData: Partial<CreatePostInput> = {};
58+
if (title !== undefined) updateData.title = title;
59+
if (description !== undefined) updateData.description = description;
60+
if (tags !== undefined) updateData.tags = tags;
61+
if (media !== undefined) updateData.media = media;
62+
63+
const updated = await Post.findByIdAndUpdate(postId, { $set: updateData }, { new: true });
64+
65+
return res.status(200).json({
66+
success: true,
67+
message: "Post updated successfully",
68+
data: updated,
69+
});
70+
} catch (error) {
71+
return res.status(500).json({ message: "Internal server error" });
72+
}
73+
};

backend/src/routes/post.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Router } from "express";
2-
import { createPost, updatePost } from "../controllers/postcontroller";
3-
import { authMiddleware } from "../middleware/authMiddleware";
2+
import { createPost, updatePost } from "../controllers/postController";
3+
import { authMiddleware } from "../middleware/auth";
44

55
const router = Router();
66

0 commit comments

Comments
 (0)