From 7cc6acf89c043fe1578121964787d9e46d5774ac Mon Sep 17 00:00:00 2001 From: MurakawaTakuya Date: Fri, 29 Nov 2024 16:03:59 +0900 Subject: [PATCH 1/4] =?UTF-8?q?put,=20delete=E3=81=AE=E3=83=86=E3=83=B3?= =?UTF-8?q?=E3=83=97=E3=83=AC=E3=83=BC=E3=83=88=E4=BD=9C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- functions/src/routers/goalRouter.ts | 4 +++ functions/src/routers/postRouter.ts | 26 +++++++++++++++++++ functions/src/routers/userRouter.ts | 39 +++++++++++++++++++++++++++++ src/app/page.tsx | 1 - 4 files changed, 69 insertions(+), 1 deletion(-) diff --git a/functions/src/routers/goalRouter.ts b/functions/src/routers/goalRouter.ts index 4559dc1..d82d490 100644 --- a/functions/src/routers/goalRouter.ts +++ b/functions/src/routers/goalRouter.ts @@ -97,4 +97,8 @@ router.route("/").post(async (req: Request, res: Response) => { } }); +// PUT: 目標を更新 + +// DELETE: 目標を削除 + export default router; diff --git a/functions/src/routers/postRouter.ts b/functions/src/routers/postRouter.ts index 826dfc0..f0bcf78 100644 --- a/functions/src/routers/postRouter.ts +++ b/functions/src/routers/postRouter.ts @@ -98,4 +98,30 @@ router.post("/", async (req: Request, res: Response) => { } }); +// PUT: 投稿を更新 +router.put("/:postId", async (req: Request, res: Response) => { + const postId = req.params.postId; + + let userId: Post["userId"]; + let storeId: Post["storeId"]; + let text: Post["text"]; + let goalId: Post["goalId"]; +}); + +// DELETE: 投稿を削除 +router.delete("/:postId", async (req: Request, res: Response) => { + const postId = req.params.postId; + + if (!postId) { + return res.status(400).json({ message: "Post ID is required" }); + } + + try { + await db.collection("post").doc(postId).delete(); + return res.json({ message: "Post deleted successfully" }); + } catch (error) { + return res.status(500).json({ message: "Error deleting post", error }); + } +}); + export default router; diff --git a/functions/src/routers/userRouter.ts b/functions/src/routers/userRouter.ts index 4df9371..7713b2f 100644 --- a/functions/src/routers/userRouter.ts +++ b/functions/src/routers/userRouter.ts @@ -111,6 +111,45 @@ router.route("/").post(async (req: Request, res: Response) => { } }); +// PUT: ユーザー情報を更新 +router.route("/:userId").put(async (req: Request, res: Response) => { + const userId = req.params.userId; + const { name, streak }: Partial = req.body; + + if (!name && streak === undefined) { + return res + .status(400) + .json({ message: "At least one of name or streak is required" }); + } + + const updateData: Partial = {}; + if (name) updateData.name = name; + if (streak !== undefined) updateData.streak = streak; + + try { + await db.collection("user").doc(userId).update(updateData); + return res.json({ message: "User updated successfully", userId }); + } catch (error) { + return res.status(500).json({ message: "Error updating user", error }); + } +}); + +// DELETE: ユーザーを削除 +router.route("/:userId").delete(async (req: Request, res: Response) => { + const userId = req.params.userId; + + if (!userId) { + return res.status(400).json({ message: "User ID is required" }); + } + + try { + await db.collection("user").doc(userId).delete(); + return res.json({ message: "User deleted successfully", userId }); + } catch (error) { + return res.status(500).json({ message: "Error deleting user", error }); + } +}); + export default router; // ユーザー名からユーザー情報を取得 diff --git a/src/app/page.tsx b/src/app/page.tsx index c52875b..0576d5f 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -13,7 +13,6 @@ export default function Top() { return ( <> - asdf