|
| 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.post("/", async (req: Request, res: Response) => { |
| 34 | + const { name } = req.body; |
| 35 | + if (typeof name !== "string") { |
| 36 | + return res.status(400).json({ error: "name must be a string" }); |
| 37 | + } |
| 38 | + try { |
| 39 | + const newSubject = await interest.create(name); |
| 40 | + res.status(201).json(newSubject); |
| 41 | + } catch (error) { |
| 42 | + console.error("Error creating subject:", error); |
| 43 | + res.status(500).json({ error: "Failed to create subject" }); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +router.patch("/mine", async (req: Request, res: Response) => { |
| 48 | + const userId = await safeGetUserId(req); |
| 49 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 50 | + const { subjectId } = req.body; |
| 51 | + try { |
| 52 | + const newSubject = await interest.get(subjectId); |
| 53 | + if (!newSubject) { |
| 54 | + return res.status(404).json({ error: "Subject not found" }); |
| 55 | + } |
| 56 | + } catch (err) { |
| 57 | + console.error("Error fetching subject:", err); |
| 58 | + res.status(500).json({ error: "Failed to fetch subject" }); |
| 59 | + } |
| 60 | + try { |
| 61 | + const updatedSubjects = await interest.add(userId.value, subjectId); |
| 62 | + res.status(200).json(updatedSubjects); |
| 63 | + } catch (error) { |
| 64 | + console.error("Error updating subjects:", error); |
| 65 | + res.status(500).json({ error: "Failed to update subjects" }); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +router.delete("/mine", async (req: Request, res: Response) => { |
| 70 | + const userId = await safeGetUserId(req); |
| 71 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 72 | + const { subjectId } = req.body; |
| 73 | + try { |
| 74 | + const newSubject = await interest.get(subjectId); |
| 75 | + if (!newSubject) { |
| 76 | + return res.status(404).json({ error: "Subject not found" }); |
| 77 | + } |
| 78 | + } catch (err) { |
| 79 | + console.error("Error fetching subject:", err); |
| 80 | + res.status(500).json({ error: "Failed to fetch subject" }); |
| 81 | + } |
| 82 | + try { |
| 83 | + const updatedSubjects = await interest.remove(userId.value, subjectId); |
| 84 | + res.status(200).json(updatedSubjects); |
| 85 | + } catch (error) { |
| 86 | + console.error("Error deleting subjects:", error); |
| 87 | + res.status(500).json({ error: "Failed to delete subjects" }); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +router.put("/mine", async (req: Request, res: Response) => { |
| 92 | + const userId = await safeGetUserId(req); |
| 93 | + if (!userId.ok) return res.status(401).send("auth error"); |
| 94 | + const { subjectIds } = req.body; |
| 95 | + if (!Array.isArray(subjectIds)) { |
| 96 | + return res.status(400).json({ error: "subjectIds must be an array" }); |
| 97 | + } |
| 98 | + try { |
| 99 | + const newSubjects = await Promise.all( |
| 100 | + subjectIds.map((id) => interest.get(id)), |
| 101 | + ); |
| 102 | + if (newSubjects.some((s) => !s)) { |
| 103 | + return res.status(404).json({ error: "Subject not found" }); |
| 104 | + } |
| 105 | + } catch (err) { |
| 106 | + console.error("Error fetching subjects:", err); |
| 107 | + res.status(500).json({ error: "Failed to fetch subjects" }); |
| 108 | + } |
| 109 | + try { |
| 110 | + const updatedSubjects = await interest.updateMultipleWithTransaction( |
| 111 | + userId.value, |
| 112 | + subjectIds, |
| 113 | + ); |
| 114 | + res.status(200).json(updatedSubjects); |
| 115 | + } catch (error) { |
| 116 | + console.error("Error updating subjects:", error); |
| 117 | + res.status(500).json({ error: "Failed to update subjects" }); |
| 118 | + } |
| 119 | +}); |
| 120 | + |
| 121 | +router.get("/all", async (req: Request, res: Response) => { |
| 122 | + try { |
| 123 | + const subjects = await interest.all(); |
| 124 | + res.status(200).json(subjects); |
| 125 | + } catch (error) { |
| 126 | + console.error("Error fetching subjects:", error); |
| 127 | + res.status(500).json({ error: "Failed to fetch subjects" }); |
| 128 | + } |
| 129 | +}); |
| 130 | + |
| 131 | +export default router; |
0 commit comments