HotTRDealsBackend/routes/deal/voteRoutes.js

52 lines
1.3 KiB
JavaScript
Raw 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 { voteDeal, getVotes } = require("../../services/deal/dealService")
const { z } = require("zod")
const router = express.Router()
// Şema tanımı
const voteSchema = z.object({
dealId: z.number().int().positive(),
voteType: z.enum(["UP", "DOWN"]),
})
// Oy verme
router.post("/", authMiddleware, async (req, res) => {
const parsed = voteSchema.safeParse(req.body)
if (!parsed.success) {
return res.status(400).json({
error: "Geçersiz veri",
details: parsed.error.errors.map((e) => e.message),
})
}
const { dealId, voteType } = parsed.data
const userId = req.user.userId
try {
const score = await voteDeal(dealId, userId, voteType)
res.json({ score })
} catch (err) {
console.error(err)
res.status(500).json({ error: "Sunucu hatası" })
}
})
// Belirli deal için oyları çek
router.get("/:dealId", async (req, res) => {
try {
const dealId = Number(req.params.dealId)
if (isNaN(dealId) || dealId <= 0)
return res.status(400).json({ error: "Geçersiz dealId" })
const data = await getVotes(dealId)
res.json(data)
} catch (err) {
console.error(err)
res.status(500).json({ error: "Sunucu hatası" })
}
})
module.exports = router