Allow users to edit and preview their posts before publishing

Add API endpoints to fetch, update post content, and manage associated media for posts.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda
Replit-Commit-Checkpoint-Type: intermediate_checkpoint
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/ER8Naji
This commit is contained in:
michaelschal committed 2025-10-09 08:21:47 +00:00
1 parent 5439566577
commit db67f4d039
1 file changed
+79
+79
View File
@@ -653,6 +653,85 @@ export async function registerRoutes(app: Express): Promise<Server> {
}
});
app.get("/api/posts/:id", requireAuth, async (req, res) => {
try {
const user = req.user as User;
const userId = user.id;
const { id } = req.params;
const postWithMedia = await storage.getPostWithMedia(id);
if (!postWithMedia) {
return res.status(404).json({ error: "Post not found" });
}
if (postWithMedia.post.userId !== userId) {
return res.status(403).json({ error: "Unauthorized" });
}
res.json(postWithMedia);
} catch (error) {
console.error("Error fetching post:", error);
res.status(500).json({ error: "Failed to fetch post" });
}
});
app.patch("/api/posts/:id", requireAuth, async (req, res) => {
try {
const user = req.user as User;
const userId = user.id;
const { id } = req.params;
const { content } = req.body;
const post = await storage.getPost(id);
if (!post) {
return res.status(404).json({ error: "Post not found" });
}
if (post.userId !== userId) {
return res.status(403).json({ error: "Unauthorized" });
}
const updatedPost = await storage.updatePost(id, { content });
res.json(updatedPost);
} catch (error) {
console.error("Error updating post:", error);
res.status(500).json({ error: "Failed to update post" });
}
});
app.patch("/api/posts/:id/media", requireAuth, async (req, res) => {
try {
const user = req.user as User;
const userId = user.id;
const { id } = req.params;
const { mediaIds } = req.body;
const post = await storage.getPost(id);
if (!post) {
return res.status(404).json({ error: "Post not found" });
}
if (post.userId !== userId) {
return res.status(403).json({ error: "Unauthorized" });
}
if (!Array.isArray(mediaIds)) {
return res.status(400).json({ error: "mediaIds must be an array" });
}
if (mediaIds.length > 10) {
return res.status(400).json({ error: "Maximum 10 photos autorisées par publication" });
}
await storage.updatePostMedia(id, mediaIds);
const updatedPostWithMedia = await storage.getPostWithMedia(id);
res.json(updatedPostWithMedia);
} catch (error) {
console.error("Error updating post media:", error);
res.status(500).json({ error: "Failed to update post media" });
}
});
// Scheduled posts
app.get("/api/scheduled-posts", requireAuth, async (req, res) => {
try {