CentralBackend/repositories/subscriptions/CoffeeRepository.js

82 lines
2.6 KiB
JavaScript

const db = require('../../database/database');
const { logger } = require('../../middleware/logger');
class CoffeeRepository {
async listAll(conn) {
const cx = conn || db;
const [rows] = await cx.query('SELECT * FROM coffee_table ORDER BY id DESC');
return rows || [];
}
async getById(id, conn) {
const cx = conn || db;
const [rows] = await cx.query('SELECT * FROM coffee_table WHERE id = ? LIMIT 1', [id]);
return rows && rows[0] ? rows[0] : null;
}
async create(data, conn) {
const cx = conn || db;
const sql = `INSERT INTO coffee_table (
title, description, price, currency, is_featured,
billing_interval, interval_count,
object_storage_id, original_filename,
state, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())`;
const params = [
data.title,
data.description,
data.price,
data.currency,
data.is_featured,
data.billing_interval,
data.interval_count,
data.object_storage_id,
data.original_filename,
data.state
];
const [result] = await cx.query(sql, params);
logger.info('[CoffeeRepository.create] insert', { id: result.insertId });
return { id: result.insertId, ...data };
}
async update(id, data, conn) {
const cx = conn || db;
const sql = `UPDATE coffee_table
SET title = ?, description = ?, price = ?, currency = ?, is_featured = ?,
billing_interval = ?, interval_count = ?,
object_storage_id = ?, original_filename = ?,
state = ?, updated_at = NOW()
WHERE id = ?`;
const params = [
data.title,
data.description,
data.price,
data.currency,
data.is_featured,
data.billing_interval,
data.interval_count,
data.object_storage_id,
data.original_filename,
data.state,
id
];
const [result] = await cx.query(sql, params);
logger.info('[CoffeeRepository.update] update', { id, affected: result.affectedRows });
return result.affectedRows > 0;
}
async setState(id, state, conn) {
const cx = conn || db;
const [result] = await cx.query('UPDATE coffee_table SET state = ?, updated_at = NOW() WHERE id = ?', [state, id]);
return result.affectedRows > 0;
}
async delete(id, conn) {
const cx = conn || db;
const [result] = await cx.query('DELETE FROM coffee_table WHERE id = ?', [id]);
return result.affectedRows > 0;
}
}
module.exports = new CoffeeRepository();