From db67f4d03952bfdda3561ed83e39edfa658f52c7 Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Thu, 9 Oct 2025 08:21:47 +0000 Subject: [PATCH] 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 --- server/routes.ts | 79 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/server/routes.ts b/server/routes.ts index 6282a33..6e418ce 100644 --- a/server/routes.ts +++ b/server/routes.ts @@ -653,6 +653,85 @@ export async function registerRoutes(app: Express): Promise { } }); + 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 {