const query = require('../db/db-query');

module.exports = {
    get: async (minuteid) => {
        let sql = '';
        minuteid
            ? (sql = `SELECT * FROM meeting_records where MEETING_ID = ${minuteid}`)
            : (sql = `SELECT * FROM meeting_records ORDER BY MEETING_ID DESC LIMIT 1`);
        try {
            const minuteData = await query(sql);
            return minuteData;
        } catch (err) {
            return err;
        }
    },
    post: async (minute) => {
        let sql = `INSERT INTO meeting_records
        (MEETING_TITLE,MEETING_NUMBER,MEETING_CHARPERSON,MEETING_MINUTE,MEETING_VENUE,MEETING_PARTICIPANT,MEETING_TAGS,MEETING_DATE,MEETING_TIME,MEETING_TEXT,MEETING_DOC_NAME,MEETING_ATTACH_NAME)
        values('${minute.title}',${minute.number},'${minute.charperson}' , '${minute.minute}', '${minute.venue}', '${minute.participant}',' ${minute.tags}', '${minute.date}', '${minute.time}','${minute.minuteText}','${minute.docFileName}','${minute.attachFilesName}')`;
        try {
            await query(sql);
            return;
        } catch (err) {
            return err;
        }
    },
    put: async (minuteData) => {
        try {
            for (let key in minuteData) {
                if (key == 'minuteID') continue;
                if (minuteData[key] || key == 'tags' || key == 'attach_name') {
                    let sql = `UPDATE meeting_records SET meeting_${key} = '${minuteData[key]}' WHERE MEETING_ID = ${minuteData.minuteID}`;
                    await query(sql);
                }
            }
            return;
        } catch (err) {
            return err;
        }
    },
    delete: async (Meeting_ID) => {
        let sql = `DELETE FROM meeting_records  WHERE MEETING_ID=${Meeting_ID}`;
        try {
            await query(sql);
            return;
        } catch (err) {
            return err;
        }
    },
};
