50 lines
1.8 KiB
JavaScript
50 lines
1.8 KiB
JavaScript
const express = require("express");
|
||
const categoryService = require("../services/category.service"); // Kategori servisi
|
||
const router = express.Router();
|
||
const { mapCategoryToCategoryDetailsResponse }=require("../adapters/responses/categoryDetails.adapter")
|
||
const { mapDealToDealDetailResponse } = require("../adapters/responses/dealDetail.adapter")
|
||
const { mapPaginatedDealsToDealCardResponse } = require("../adapters/responses/dealCard.adapter")
|
||
|
||
|
||
router.get("/:slug", async (req, res) => {
|
||
const { slug } = req.params; // URL parametresinden slug alıyoruz
|
||
try {
|
||
|
||
const { category, breadcrumb } = await categoryService.findCategoryBySlug(slug); // Servisten kategori ve breadcrumb bilgilerini alıyoruz
|
||
if (!category) {
|
||
return res.status(404).json({ error: "Kategori bulunamadı" });
|
||
}
|
||
const response = mapCategoryToCategoryDetailsResponse(category, breadcrumb); // Adapter ile veriyi dönüştürüyoruz
|
||
res.json(response);
|
||
} catch (err) {
|
||
res.status(500).json({ error: "Kategori detayları alınırken bir hata oluştu", message: err.message });
|
||
}
|
||
});
|
||
|
||
|
||
router.get("/:slug/deals", async (req, res) => {
|
||
const { slug } = req.params;
|
||
const { page = 1, limit = 10, ...filters } = req.query;
|
||
|
||
try {
|
||
const { category } = await categoryService.findCategoryBySlug(slug);
|
||
if (!category) {
|
||
return res.status(404).json({ error: "Kategori bulunamadı" });
|
||
}
|
||
|
||
// Kategorinin fırsatlarını alıyoruz
|
||
const deals = await categoryService.getDealsByCategoryId(category.id, page, limit, filters);
|
||
|
||
const response = mapPaginatedDealsToDealCardResponse(payload)
|
||
|
||
|
||
// frontend DealCard[] bekliyor
|
||
res.json(response.results)
|
||
} catch (err) {
|
||
res.status(500).json({ error: "Kategoriye ait fırsatlar alınırken bir hata oluştu", message: err.message });
|
||
}
|
||
});
|
||
|
||
|
||
module.exports = router;
|