59 lines
1.5 KiB
JavaScript
59 lines
1.5 KiB
JavaScript
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
|