refactor(chart): Fixed tabulation and code duplicates

This commit is contained in:
Данил 2024-12-14 23:36:01 +03:00
parent 85acbbb16c
commit ded9abc816
2 changed files with 31 additions and 29 deletions

View file

@ -7,28 +7,28 @@ router = APIRouter()
@router.get("/api/getChart/", status_code=status.HTTP_201_CREATED) @router.get("/api/getChart/", status_code=status.HTTP_201_CREATED)
async def get_chart( async def get_chart(
response: Response, response: Response,
request: Request, request: Request,
from_currency: str = None, from_currency: str = None,
conv_currency: str = None, conv_currency: str = None,
start_date: str = None, start_date: str = None,
end_date: str = None, end_date: str = None,
): ):
if not from_currency or not conv_currency: if not from_currency or not conv_currency:
response.status_code = status.HTTP_400_BAD_REQUEST response.status_code = status.HTTP_400_BAD_REQUEST
return { return {
'status': status.HTTP_400_BAD_REQUEST, 'status': status.HTTP_400_BAD_REQUEST,
'message': 'The from_currency and conv_currency fields are required.', 'message': 'The from_currency and conv_currency fields are required.',
} }
elif not start_date and not end_date: elif not start_date and not end_date:
response.status_code = status.HTTP_400_BAD_REQUEST response.status_code = status.HTTP_400_BAD_REQUEST
return { return {
'status': status.HTTP_400_BAD_REQUEST, 'status': status.HTTP_400_BAD_REQUEST,
'message': 'The start_date and end_date fields are required.', 'message': 'The start_date and end_date fields are required.',
} }
chart = await create_chart(from_currency, conv_currency, start_date, end_date) chart = await create_chart(from_currency, conv_currency, start_date, end_date)
return await prepare_chart_response(response, request, chart) return await prepare_chart_response(response, request, chart)

View file

@ -9,19 +9,19 @@ router = APIRouter()
@router.get("/api/getChart/{period}", status_code=status.HTTP_201_CREATED) @router.get("/api/getChart/{period}", status_code=status.HTTP_201_CREATED)
async def get_chart_period( async def get_chart_period(
response: Response, response: Response,
request: Request, request: Request,
from_currency: str = None, from_currency: str = None,
conv_currency: str = None, conv_currency: str = None,
period: str = None, period: str = None,
): ):
if not from_currency or not conv_currency: if not from_currency or not conv_currency:
response.status_code = status.HTTP_400_BAD_REQUEST response.status_code = status.HTTP_400_BAD_REQUEST
return { return {
'status': status.HTTP_400_BAD_REQUEST, 'status': status.HTTP_400_BAD_REQUEST,
'message': 'The from_currency and conv_currency fields are required.', 'message': 'The from_currency and conv_currency fields are required.',
} }
if period not in ['week', 'month', 'quarter', 'year']: if period not in ['week', 'month', 'quarter', 'year']:
response.status_code = status.HTTP_400_BAD_REQUEST response.status_code = status.HTTP_400_BAD_REQUEST
@ -41,14 +41,16 @@ async def get_chart_period(
end_date = datetime.now() end_date = datetime.now()
start_date = end_date + dateutil.relativedelta.relativedelta(months=month, days=days, years=years) start_date = end_date + dateutil.relativedelta.relativedelta(months=month, days=days, years=years)
chart = await create_chart(from_currency, chart = await create_chart(
conv_currency, from_currency,
start_date.strftime('%Y-%m-%d'), conv_currency,
end_date.strftime('%Y-%m-%d') start_date.strftime('%Y-%m-%d'),
) end_date.strftime('%Y-%m-%d')
)
return await prepare_chart_response(response, request, chart) return await prepare_chart_response(response, request, chart)
async def prepare_chart_response(response: Response, request: Request, chart_name: str): async def prepare_chart_response(response: Response, request: Request, chart_name: str):
if not chart_name: if not chart_name:
response.status_code = status.HTTP_404_NOT_FOUND response.status_code = status.HTTP_404_NOT_FOUND