Series: prisma
javascript
77 lines
· Updated 2026-02-03
comments.js
prisma/src/routes/comments.js
/**
* Comments routes: standalone CRUD with relations.
*/
const express = require('express');
const router = express.Router();
const { prisma } = require('../prisma');
module.exports = router;
// GET /comments
router.get('/', async (req, res, next) => {
try {
const comments = await prisma.comment.findMany({
include: { author: true, post: true },
orderBy: { id: 'asc' },
});
res.json({ data: comments });
} catch (err) { next(err); }
});
// GET /comments/:id
router.get('/:id', async (req, res, next) => {
try {
const id = Number(req.params.id);
const comment = await prisma.comment.findUnique({
where: { id },
include: { author: true, post: true },
});
if (!comment) return res.status(404).json({ error: { message: 'Comment not found' } });
res.json({ data: comment });
} catch (err) { next(err); }
});
// POST /comments { content, postId, authorId|authorEmail }
router.post('/', async (req, res, next) => {
try {
const { content, authorId, authorEmail, postId } = req.body;
if (!content || !postId) {
const e = new Error('content and postId are required'); e.status = 400; throw e;
}
const author = authorId
? { connect: { id: Number(authorId) } }
: (authorEmail ? { connect: { email: authorEmail } } : null);
if (!author) { const e = new Error('authorId or authorEmail is required'); e.status = 400; throw e; }
const created = await prisma.comment.create({
data: { content, post: { connect: { id: Number(postId) } }, author },
include: { author: true, post: true },
});
res.status(201).json({ data: created });
} catch (err) { next(err); }
});
// PATCH /comments/:id
router.patch('/:id', async (req, res, next) => {
try {
const id = Number(req.params.id);
const { content } = req.body;
const updated = await prisma.comment.update({
where: { id },
data: { content },
select: { id: true, content: true, updatedAt: true },
});
res.json({ data: updated });
} catch (err) { next(err); }
});
// DELETE /comments/:id
router.delete('/:id', async (req, res, next) => {
try {
const id = Number(req.params.id);
await prisma.comment.delete({ where: { id } });
res.status(204).send();
} catch (err) { next(err); }
});
module.exports = router;
Related articles
prisma
sql
Updated 2026-02-03
migration.sql
migration.sql — sql source code from the prisma learning materials (prisma/prisma/migrations/20250824120014_init/migration.sql).
Read article →
prisma
javascript
Updated 2026-02-03
seed.js
seed.js — javascript source code from the prisma learning materials (prisma/prisma/seed.js).
Read article →
prisma
javascript
Updated 2026-02-03
index.js
index.js — javascript source code from the prisma learning materials (prisma/src/index.js).
Read article →
prisma
javascript
Updated 2026-02-03
errorHandler.js
errorHandler.js — javascript source code from the prisma learning materials (prisma/src/middleware/errorHandler.js).
Read article →
prisma
javascript
Updated 2026-02-03
prisma.js
prisma.js — javascript source code from the prisma learning materials (prisma/src/prisma.js).
Read article →
prisma
javascript
Updated 2026-02-03
posts.js
posts.js — javascript source code from the prisma learning materials (prisma/src/routes/posts.js).
Read article →