← back to Watches

api/exchange-rates.js

20 lines

import { getExchangeRates, getRate, convertCurrency } from '../services/exchange-rates.js';

export async function getRates(req, res) {
  const rates = await getExchangeRates();
  res.json({ success: true, rates, lastUpdated: new Date().toISOString() });
}

export async function getSpecificRate(req, res) {
  const rate = await getRate(req.params.currency);
  res.json({ success: true, currency: req.params.currency, rate });
}

export async function convert(req, res) {
  const { amount, from, to } = req.query;
  const converted = await convertCurrency(parseFloat(amount), from, to);
  res.json({ success: true, amount: parseFloat(amount), from, to, converted, rate: converted / parseFloat(amount) });
}

export default { getRates, getSpecificRate, convert };