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

class GetCategories extends API {
    constructor() {
        super();

        this.setPath('/Goods/GetCategories')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        let sql = `
            SELECT
                name,
                value
            FROM
                goods_categorie
            WHERE state = 1
        `
        let rows = await db.query(sql)
        if (!rows || rows.length === 0)
            return res.json({
                ...BaseStdResponse.MISSING_FILE,
                msg: '获取商品分类失败！'
            })

        res.json({
            ...BaseStdResponse.OK,
            data: rows
        })
    }
}

module.exports.GetCategories = GetCategories