Skip to content

Commit

Permalink
Merge pull request #69 from ASparton/feat/23-get-one-crypto-history
Browse files Browse the repository at this point in the history
feat/23-get-one-crypto-history
  • Loading branch information
alexis-moins authored Dec 4, 2023
2 parents 7b98558 + f71f2fe commit b34973a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 14 deletions.
5 changes: 5 additions & 0 deletions back/src/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ enum ApiErrors {
* Raised when the resource at the given id was not found by prisma.
*/
RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND',

/**
* Raised when the request is not in a valid format.
*/
BAD_REQUEST = 'BAD_REQUEST',
}

export class APIError extends Error {
Expand Down
63 changes: 58 additions & 5 deletions back/src/composables/useCrypto.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,71 @@
import { binance } from 'ccxt';
import HttpStatusCode from '#types/HttpStatusCode';
import ApiErrors, { APIError } from '~apiErrors';

import { binance } from 'ccxt';
const _exchange = new binance();

async function getAllCrypto(cryptos: string[]) {
return await _exchange.fetchTickers(cryptos);
/**
* Return the period and limit associated with a given period
*
* @param period - either 'daily', 'hourly' or 'minute'
*
* @returns a tuple of string and number
*/
function _getPeriodAndLimit(period: string): [string, number] {
switch (period) {
case 'daily':
return ['1d', 60];

case 'hourly':
return ['1h', 48];

case 'minute':
return ['1m', 120];

default:
throw new APIError(ApiErrors.BAD_REQUEST, HttpStatusCode.BAD_REQUEST_400);
}
}

/**
* Returns the tickers associated with the given symbols.
*
* @param symbols - symbols to fetch
*
* @returns a Promise of Ticker array
*/
async function getAllCrypto(symbols: string[]) {
return await _exchange.fetchTickers(symbols);
}

/**
* Returns the ticker associated with the given symbol.
*
* @param symbol - symbol to fetch
*
* @returns a Promise of Ticker
*/
async function getCrypto(symbol: string) {
return await _exchange.fetchTicker(symbol);
}

async function getCrypto(crypto: string) {
return await _exchange.fetchTicker(crypto);
/**
* Retrieves the history of the symbol using the given period.
*
* @param symbol - symbol to fetch
* @param period - period to use
*
* @returns a Promise of an OHLCV array
*/
async function getHistory(symbol: string, period: string) {
const [_period, limit] = _getPeriodAndLimit(period);
return await _exchange.fetchOHLCV(symbol, _period, undefined, limit);
}

const useCrypto = () => ({
getAllCrypto,
getCrypto,
getHistory,
});

export default useCrypto;
30 changes: 21 additions & 9 deletions back/src/controllers/cryptos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
} from '@database/cryptos';

import { adminRoleRequired, authenticationRequired } from '~middlewares';
import HistoryParamDTO from '#types/dto/cryptos/HistoryParamDTO';

const { getAllCrypto, getCrypto } = useCrypto();
const { getAllCrypto, getCrypto, getHistory } = useCrypto();
const controller = express.Router();

controller.get('/', async (req, res) => {
Expand Down Expand Up @@ -66,7 +67,7 @@ controller.get('/', async (req, res) => {
return res.status(HttpStatusCode.OK_200).send(response);
});

controller.get('/:id', async (req, res) => {
controller.get('/:id', authenticationRequired, async (req, res) => {
const params = UrlParamIdDTO.parse(req.params);
const currency = req.lucia ? req.lucia.user.currency : 'EUR';

Expand All @@ -83,13 +84,24 @@ controller.get('/:id', async (req, res) => {
});
});

// controller.get('/:id/history/:period', async (req, res) => {
// const urlParams = UrlParamIdDTO.parse(req.params);
// const body = UpdateFeedDto.parse(req.body);
// return res
// .status(HttpStatusCode.OK_200)
// .send(await updateFeedById(urlParams.id, body.minArticlesCount));
// });
controller.get(
'/:id/history/:period',
authenticationRequired,
async (req, res) => {
const params = HistoryParamDTO.parse(req.params);
const currency = req.lucia ? req.lucia.user.currency : 'EUR';

const crypto = await findCryptoById(params.id);
const apiResponse = await getHistory(
`${crypto.api_id}/${currency}`,
params.period,
);

console.log(apiResponse.length);

return res.status(HttpStatusCode.OK_200).send(apiResponse);
},
);

controller.post(
'/',
Expand Down
8 changes: 8 additions & 0 deletions back/src/types/dto/cryptos/HistoryParamDTO.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { z } from 'zod';

const HistoryParamDTO = z.object({
id: z.coerce.number(),
period: z.enum(['daily', 'hourly', 'minute']),
});

export default HistoryParamDTO;

0 comments on commit b34973a

Please sign in to comment.