נמשיך בפיתוח הראוטים של שירות ה-Notebooks.

ודאו שכל המערכת רצה מה-Root:

docker compose up --build --watch

ובדקו שהבקשות דרך:

<http://localhost:8080/api/notebooks>

עובדות כרגיל.


חלק ראשון – שליפת Notebook לפי ID

שקופית 1 – מימוש GET /:id

נעתיק את שלד ה-GET הקודם ונעדכן:

notebookRouter.get("/:id", async (req, res) => {
  try {
    const { id } = req.params;

    const notebook = await Notebook.findById(id);

    if (!notebook) {
      return res.status(404).json({
        error: "Notebook not found",
      });
    }

    res.json({
      data: notebook,
    });
  } catch (error) {
    res.status(500).json({
      error: error.message,
    });
  }
});

שקופית 2 – בעיית ObjectId לא תקין

כאשר שולחים ID לא תקין (למשל abc),

Mongoose זורק שגיאה פנימית.

מבחינת משתמש API — זו חוויית משתמש לא טובה.

נוסיף בדיקה מוקדמת:

const mongoose = require("mongoose");

ולפני findById:

if (!mongoose.Types.ObjectId.isValid(id)) {
  return res.status(404).json({
    error: "Notebook not found",
  });
}