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

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

        this.setPath('/Public/GetNotice')
        this.setMethod('GET')
    }

    async onRequest(req, res) {
        let { key } = req.query

        if ([key].some(value => value === '' || value === null || value === undefined))
            return res.json({
                ...BaseStdResponse.MISSING_PARAMETER
            })

        let sql = 'SELECT content FROM notice WHERE \`key\` = ?'
        let rows = await db.query(sql, [key])

        if (!rows || rows.length === 0)
            return res.json({
                ...BaseStdResponse.OK,
                msg: ''
            })

        res.json({
            ...BaseStdResponse.OK,
            msg: rows[0].content || ''
        })
    }
}

module.exports.GetNotice = GetNotice