добавлена новая ошибка, код стал чуть читабельнее

This commit is contained in:
Данил 2024-02-19 17:18:32 +03:00
parent 12ce6b7522
commit 01e61308b9

28
main.js
View file

@ -1,35 +1,49 @@
const fastify = require('fastify')({ logger: true }); const fastify = require('fastify')({ logger: true });
const schedule = require('node-schedule'); const schedule = require('node-schedule');
const pool = require("./postgresql.js"); const pool = require("./postgresql.js");
const yaml = require("yaml")
const fs = require("fs");
const config = yaml.parse(fs.readFileSync("./config.yaml", "utf-8"));
const saveRate = require('./utils/saveRate.js'); const saveRate = require('./utils/saveRate.js');
const response = require('./utils/errorResponse'); const response = require('./utils/errorResponse');
schedule.scheduleJob('30 8 * * *', async function () { schedule.scheduleJob('30 8 * * *', async function () {
console.log('I save the currency data'); console.log('I save the currency data');
await saveRate(); await saveRate();
}); });
fastify.get('/api/getRate/', async function (req, reply) { fastify.get('/api/getRate/', async function (req, reply) {
if (!req.query?.codeCurrency) return response( if (!req['query']?.['codeCurrency']) return response(
400, 400,
'error', 'error',
'codeCurrency parameter is required' 'codeCurrency parameter is required'
); );
if (!req.query?.periodStart) return response(400, 'error', 'period parameter is required'); if (!req['query']?.['periodStart']) return response(
400,
'error',
'periodStart parameter is required'
);
const data = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2', [ const data = await pool.query('SELECT * FROM currency WHERE from_currency = $1 AND date = $2', [
req.query.codeCurrency, req['query']['codeCurrency'],
req.query.periodStart req['query']['periodStart'],
]).then(response('error', 500, 'Internal Server Error')); ]).then(response('error', 500, 'Internal Server Error'));
console.log(typeof data['rows']) if (!data['rows']?.[0]) return response(
'error',
204,
'There is no data for this time'
);
return data['rows']; return data['rows'];
}); });
fastify.listen({ port: 3000 }, err => { fastify.listen({
host: config['server']['host'],
port: config['server']['port']
}, err => {
if (err) throw err if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`) console.log(`server listening on ${fastify.server.address().port}`)
}) })