HotTRDealsBackend/routes/deal/commentRoutes.js

47 lines
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require("express")
const authMiddleware = require("../../middleware/authMiddleware")
const {
getCommentsByDealId,
createComment,
deleteComment,
} = require("../../services/deal/commentService")
const router = express.Router()
router.get("/:dealId", async (req, res) => {
try {
const comments = await getCommentsByDealId(req.params.dealId)
res.json(comments)
} catch (err) {
console.error(err)
res.status(500).json({ error: "Sunucu hatası" })
}
})
router.post("/", authMiddleware, async (req, res) => {
try {
const { dealId, text } = req.body
const userId = req.user.userId
if (!text?.trim()) return res.status(400).json({ error: "Yorum boş olamaz." })
const comment = await createComment({ dealId, userId, text })
res.json(comment)
} catch (err) {
console.error(err)
res.status(500).json({ error: err.message || "Sunucu hatası" })
}
})
router.delete("/:id", authMiddleware, async (req, res) => {
try {
const result = await deleteComment(req.params.id, req.user.userId)
res.json(result)
} catch (err) {
console.error(err)
const status = err.message.includes("yetkin") ? 403 : 404
res.status(status).json({ error: err.message })
}
})
module.exports = router