"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const index_1 = require("../index");
const auth_1 = require("../middleware/auth");
const router = (0, express_1.Router)();
// GET /api/categories - Public
router.get('/', async (req, res) => {
    try {
        const categories = await index_1.prisma.articleCategory.findMany({ orderBy: { name: 'asc' } });
        res.json(categories);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengambil kategori', error: error instanceof Error ? error.message : '' });
    }
});
// POST /api/categories - Admin
router.post('/', auth_1.authMiddleware, async (req, res) => {
    try {
        const { name, slug } = req.body;
        if (!name || !slug)
            return res.status(400).json({ message: 'Nama dan slug diperlukan' });
        const category = await index_1.prisma.articleCategory.create({ data: { name, slug } });
        res.status(201).json(category);
    }
    catch (error) {
        if (error.code === 'P2002')
            return res.status(400).json({ message: 'Slug sudah ada' });
        res.status(500).json({ message: 'Gagal membuat kategori', error: error.message });
    }
});
// PUT /api/categories/:id - Admin
router.put('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        const category = await index_1.prisma.articleCategory.update({
            where: { id: parseInt(req.params.id) },
            data: req.body,
        });
        res.json(category);
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal mengupdate kategori', error: error instanceof Error ? error.message : '' });
    }
});
// DELETE /api/categories/:id - Admin
router.delete('/:id', auth_1.authMiddleware, async (req, res) => {
    try {
        await index_1.prisma.articleCategory.delete({ where: { id: parseInt(req.params.id) } });
        res.json({ message: 'Kategori berhasil dihapus' });
    }
    catch (error) {
        res.status(500).json({ message: 'Gagal menghapus kategori', error: error instanceof Error ? error.message : '' });
    }
});
exports.default = router;
//# sourceMappingURL=categories.js.map