import dbClient from "../db/index.js"; export default class OptionController { static async getAll() { let queryText = 'SELECT * FROM blog_options'; try { const res = await dbClient.query(queryText); return res.rows; } catch (err) { console.log(err); return []; } } static async get(name) { const res = await dbClient.query({ text: 'SELECT value FROM blog_options WHERE name=$1', values: [name] }); if (res.rows[0]) { return res.rows[0].value; } console.log('[OptionController] cannot find option ', name); return null; } static async create(name, value) { const res = await dbClient.query({ text: 'INSERT INTO blog_options(name, value) VALUES($1, $2)', values: [name, value] }); console.log('[OptionController] create:', res.rows); } static async update(name, value) { try { const res = await dbClient.query({ text: 'UPDATE blog_options SET value = $2 WHERE name = $1', values: [name, value] }); return res.rows; } catch(err) { console.error('[OptionController] update error: ', err); } } static async delete(name) { try { const res = dbClient.query({ text: 'DELETE FROM blog_options WHERE name=$1', values: [name] }); return res.rows; } catch(err) { console.error('[OptionController] delete error: ', err); } } }