Exercise 1: Adding Comments — Possible Solution ================================================= // POST /posts/:slug/comments { "body": "...", "authorEmail": "optional" } app.post("/posts/:slug/comments", async (req, res) => { const body = String(req.body.body ?? "").trim(); const authorEmail = req.body.authorEmail ? String(req.body.authorEmail) : undefined; if (!body) return res.status(400).json({ error: "body is required" }); const post = await prisma.post.findFirst({ where: { slug: req.params.slug, published: true }, select: { id: true }, }); if (!post) return res.status(404).json({ error: "Post not found" }); const comment = await prisma.comment.create({ data: { body, post: { connect: { id: post.id } }, ...(authorEmail ? { author: { connect: { email: authorEmail } } } : {}), }, select: { body: true, createdAt: true, author: { select: { name: true } } }, }); res.status(201).json(comment); }); Notes: - The post is looked up with findFirst so drafts are treated as missing, matching GET /posts/:slug. - The author is only connected when an email is given; otherwise the optional relation stays empty and it's a guest comment. - An unknown authorEmail makes the connect fail, and the error handler turns that into a 404, since connect requires the record to exist. - Only safe fields are returned, as elsewhere in the API. WHY THIS WORKS AS AN ANSWER ------------------------------ It uses nested connects for both relations, handles the optional author cleanly, and reuses the course's patterns for 404s and select.