|
| 1 | +import express, { type Request, type Response } from "express"; |
| 2 | +import * as interest from "../database/interest"; |
| 3 | +import { safeGetUserId } from "../firebase/auth/db"; |
| 4 | + |
| 5 | +const router = express.Router(); |
| 6 | + |
| 7 | +router.get("/userId/:userId", async (req: Request, res: Response) => { |
| 8 | + const userId = Number.parseInt(req.params.userId); |
| 9 | + if (Number.isNaN(userId)) { |
| 10 | + return res.status(400).json({ error: "Invalid userId" }); |
| 11 | + } |
| 12 | + try { |
| 13 | + const subjects = await interest.of(userId); |
| 14 | + res.status(200).json(subjects); |
| 15 | + } catch (error) { |
| 16 | + console.error("Error fetching subjects by userId:", error); |
| 17 | + res.status(500).json({ error: "Failed to fetch subjects by userId" }); |
| 18 | + } |
| 19 | +}); |
| 20 | + |
| 21 | +router.get("/mine", async (req: Request, res: Response) => { |
| 22 | + const userId = await safeGetUserId(req); |
| 23 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 24 | + try { |
| 25 | + const subjects = await interest.of(userId.value); |
| 26 | + res.status(200).json(subjects); |
| 27 | + } catch (error) { |
| 28 | + console.error("Error fetching subjects:", error); |
| 29 | + res.status(500).json({ error: "Failed to fetch subjects" }); |
| 30 | + } |
| 31 | +}); |
| 32 | + |
| 33 | +router.patch("/mine", async (req: Request, res: Response) => { |
| 34 | + const userId = await safeGetUserId(req); |
| 35 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 36 | + const { subjectId } = req.body; |
| 37 | + try { |
| 38 | + const newSubject = await interest.get(subjectId); |
| 39 | + if (!newSubject) { |
| 40 | + return res.status(404).json({ error: "Subject not found" }); |
| 41 | + } |
| 42 | + } catch (err) { |
| 43 | + console.error("Error fetching subject:", err); |
| 44 | + res.status(500).json({ error: "Failed to fetch subject" }); |
| 45 | + } |
| 46 | + try { |
| 47 | + const updatedSubjects = await interest.add(userId.value, subjectId); |
| 48 | + res.status(200).json(updatedSubjects); |
| 49 | + } catch (error) { |
| 50 | + console.error("Error updating subjects:", error); |
| 51 | + res.status(500).json({ error: "Failed to update subjects" }); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +router.delete("/mine", async (req: Request, res: Response) => { |
| 56 | + const userId = await safeGetUserId(req); |
| 57 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 58 | + const { subjectId } = req.body; |
| 59 | + try { |
| 60 | + const newSubject = await interest.get(subjectId); |
| 61 | + if (!newSubject) { |
| 62 | + return res.status(404).json({ error: "Subject not found" }); |
| 63 | + } |
| 64 | + } catch (err) { |
| 65 | + console.error("Error fetching subject:", err); |
| 66 | + res.status(500).json({ error: "Failed to fetch subject" }); |
| 67 | + } |
| 68 | + try { |
| 69 | + const updatedSubjects = await interest.remove(userId.value, subjectId); |
| 70 | + res.status(200).json(updatedSubjects); |
| 71 | + } catch (error) { |
| 72 | + console.error("Error deleting subjects:", error); |
| 73 | + res.status(500).json({ error: "Failed to delete subjects" }); |
| 74 | + } |
| 75 | +}); |
| 76 | + |
| 77 | +router.put("/mine", async (req: Request, res: Response) => { |
| 78 | + const userId = await safeGetUserId(req); |
| 79 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 80 | + const { subjectIds } = req.body; |
| 81 | + if (!Array.isArray(subjectIds)) { |
| 82 | + return res.status(400).json({ error: "subjectIds must be an array" }); |
| 83 | + } |
| 84 | + try { |
| 85 | + const newSubjects = await Promise.all( |
| 86 | + subjectIds.map((id) => interest.get(id)), |
| 87 | + ); |
| 88 | + if (newSubjects.some((s) => !s)) { |
| 89 | + return res.status(404).json({ error: "Subject not found" }); |
| 90 | + } |
| 91 | + } catch (err) { |
| 92 | + console.error("Error fetching subjects:", err); |
| 93 | + res.status(500).json({ error: "Failed to fetch subjects" }); |
| 94 | + } |
| 95 | + try { |
| 96 | + const updatedSubjects = await interest.updateMultipleWithTransaction( |
| 97 | + userId.value, |
| 98 | + subjectIds, |
| 99 | + ); |
| 100 | + res.status(200).json(updatedSubjects); |
| 101 | + } catch (error) { |
| 102 | + console.error("Error updating subjects:", error); |
| 103 | + res.status(500).json({ error: "Failed to update subjects" }); |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +router.get("/search/:query", async (req: Request, res: Response) => { |
| 108 | + // TODO: token との兼ね合いで、クエリパラメータでなく一旦パスパラメータとしている |
| 109 | + const q = req.params.query; |
| 110 | + if (typeof q !== "string") { |
| 111 | + return res.status(400).json({ error: "Invalid query" }); |
| 112 | + } |
| 113 | + try { |
| 114 | + const subjects = await interest.search(q); |
| 115 | + res.status(200).json(subjects); |
| 116 | + } catch (error) { |
| 117 | + console.error("Error searching subjects:", error); |
| 118 | + res.status(500).json({ error: "Failed to search subjects" }); |
| 119 | + } |
| 120 | +}); |
| 121 | + |
| 122 | +export default router; |
0 commit comments