HotTRDealsBackend/routes/deal/dealRoutes.js

40 lines
1.1 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 router = express.Router()
const { getAllDeals, getDealById, createDeal } = require("../../services/deal/dealService")
const authMiddleware = require("../../middleware/authMiddleware")
router.get("/", async (req, res) => {
try {
const page = Number(req.query.page) || 1
const limit = 10
const data = await getAllDeals(page, limit)
res.json(data)
} catch (e) {
console.error(e)
res.status(500).json({ error: "Sunucu hatası" })
}
})
router.get("/:id", async (req, res) => {
try {
const deal = await getDealById(req.params.id)
if (!deal) return res.status(404).json({ error: "Deal bulunamadı" })
res.json(deal)
} catch (e) {
console.error(e)
res.status(500).json({ error: "Sunucu hatası" })
}
})
router.post("/", authMiddleware, async (req, res) => {
try {
const userId = req.user.userId
const deal = await createDeal(req.body, userId)
res.json(deal)
} catch (err) {
console.error(err)
res.status(500).json({ error: "Sunucu hatası" })
}
})
module.exports = router