HotTRDealsBackend/routes/deal/dealRoutes.js
2025-11-05 14:56:26 +00:00

59 lines
1.5 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,searchDeals } = 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("/search", async (req, res) => {
try {
const query = req.query.q || ""
const page = Number(req.query.page) || 1
const limit = 10
if (!query.trim()) {
return res.json({ results: [], total: 0, totalPages: 0, page })
}
const data = await searchDeals(query, 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