Skip to content

Commit

Permalink
feat(back): format response correctly on GET /api/cryptos
Browse files Browse the repository at this point in the history
  • Loading branch information
alexis-moins committed Dec 4, 2023
1 parent a31a04b commit f02684e
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions back/src/controllers/cryptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,39 @@ controller.get('/', async (req, res) => {
? await findManyCryptosById(query.ids)
: await findAllVisibleCryptos();

const tickers = cryptos.map((crypto) => `${crypto.api_id}/${currency}`);
const tickers: string[] = [];

const cryptoWithTicker = cryptos.map((crypto) => {
const ticker = `${crypto.api_id}/${currency}`;

tickers.push(ticker);
return { ticker, ...crypto };
});

if (tickers.length === 0) {
return res.status(HttpStatusCode.OK_200).json([]);
}

return res.status(HttpStatusCode.OK_200).send(await getAllCrypto(tickers));
const apiResponse = await getAllCrypto(tickers);

const response = cryptoWithTicker.map((crypto) => {
const data = apiResponse[crypto.ticker];

if (!data) {
return;
}

return {
name: crypto.name,
current_price: data.last,
opening_price: data.open,
lowest_price: data.low,
highest_price: data.high,
image: crypto.logo_url,
};
});

return res.status(HttpStatusCode.OK_200).send(response);
});

// controller.get('/:id', async (req, res) => {
Expand Down

0 comments on commit f02684e

Please sign in to comment.