Добавил обработчик ошибок. Изменил поля функции под единую концепцию

This commit is contained in:
Данил 2024-07-31 14:45:12 +03:00
parent bf37441987
commit ff6ba36dbb

View file

@ -1,7 +1,8 @@
const pool = require('./postgresql.js'); const pool = require('./postgresql.js');
async function getDay(from_currency, conv_currency, date) { async function getDay(from_currency, conv_currency, date) {
if (!from_currency || !conv_currency || !date) return if (!from_currency || !conv_currency) return new Error('fromCurrency and convCurrency are required');
else if (!date) return new Error('date is required')
const data = await pool.query('SELECT from_currency, conv_currency, date, rate FROM currency ' + const data = await pool.query('SELECT from_currency, conv_currency, date, rate FROM currency ' +
'WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', [ 'WHERE from_currency = $1 AND conv_currency = $2 AND date = $3', [
@ -10,26 +11,26 @@ async function getDay(from_currency, conv_currency, date) {
date date
]); ]);
if (!data) return; if (!data) return new Error('Missing data');
return data['rows'][0]; return data['rows'][0];
} }
async function getPeriod(from_currency, conv_currency, start_date, end_date) { async function getPeriod(from_currency, conv_currency, start_date, end_date) {
if (!from_currency || !conv_currency || !start_date || !end_date) return if (!from_currency || !conv_currency) return new Error('from_currency and conv_currency are required');
else if(!start_date || !end_date) return new Error('start_date and end_date are required')
const data = await pool.query('SELECT * FROM currency WHERE ' + const data = await pool.query('SELECT * FROM currency WHERE ' +
'(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2', [ '(date BETWEEN $3 AND $4) AND from_currency = $1 AND conv_currency = $2', [
from_currency, from_currency.toUpperCase(),
conv_currency, conv_currency.toUpperCase(),
start_date, start_date,
end_date end_date
]); ]);
if (!data) return; if (!data) return new Error('Missing data');
return data['rows']; return data['rows'];
} }
module.exports = { getDay, getPeriod };
module.exports = { getDay, getPeriod };