const API = require("../../../lib/API");
const db = require("../../../plugin/DataBase/db");
const { BaseStdResponse } = require("../../../BaseStdResponse");

class GetArticleCategories extends API {
    constructor() {
        super();
        this.setPath('/Article/Categories');
        this.setMethod('GET');
    }

    async onRequest(req, res) {
        const sql = `
            SELECT name, slug, icon, sort_order
            FROM article_category
            WHERE is_active = 1
            ORDER BY sort_order ASC, id ASC
        `;
        try {
            const rows = await db.query(sql);
            res.json({
                ...BaseStdResponse.OK,
                data: rows || []
            });
        } catch (err) {
            this.logger.error(`获取文章分类失败！${err.stack}`);
            res.json({
                ...BaseStdResponse.ERR,
                msg: '获取文章分类失败！'
            });
        }
    }
}

module.exports.GetArticleCategories = GetArticleCategories;
