Skip to content

Commit 647999e

Browse files
committed
chore(chart)!: Merged two routes into one. Put the period in query params instead of url part
1 parent 3af061a commit 647999e

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

chart/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from utils.exception_handler import custom_validation_exception
1414

1515
from middleware.plausible_analytics import PlausibleAnalytics
16-
from routes import get_chart, get_chart_period
16+
from routes import get_chart
1717

1818
app = FastAPI(lifespan=lifespan)
1919
config = load_config('config.hjson')
@@ -26,8 +26,6 @@
2626
app.middleware('http')(PlausibleAnalytics())
2727

2828
app.include_router(get_chart.router)
29-
app.include_router(get_chart_period.router)
30-
3129

3230
if __name__ == '__main__':
3331
uvicorn.run(app, host=config['server']['host'], port=3030)

chart/routes/get_chart.py

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
data, and returns a formatted chart response.
77
"""
88
from datetime import datetime
9+
from dateutil.relativedelta import relativedelta
910

1011
from fastapi import APIRouter, status, Depends, HTTPException, Request
1112
from fastapi.responses import StreamingResponse
@@ -44,17 +45,37 @@ async def get_chart(req: Request, currency: Currency = Depends()) -> StreamingRe
4445
detail='The from_currency and conv_currency fields are required.'
4546
)
4647

47-
if not currency.start_date or not currency.end_date:
48-
raise HTTPException(
49-
status_code=status.HTTP_400_BAD_REQUEST,
50-
detail='The start_date and end_date fields are required.'
51-
)
48+
days, month, years = 0, 0, 0
5249

53-
if currency.start_date > currency.end_date:
54-
raise HTTPException(
55-
status_code=status.HTTP_400_BAD_REQUEST,
56-
detail="The start_date cannot be later than the end_date."
50+
if currency.period:
51+
match currency.period:
52+
case 'week':
53+
days = -7
54+
case 'month':
55+
month = -1
56+
case 'quarter':
57+
month = -3
58+
case 'year':
59+
years = -1
60+
61+
currency.end_date = datetime.now()
62+
currency.start_date = currency.end_date + relativedelta(
63+
months=month,
64+
days=days,
65+
years=years
5766
)
67+
else:
68+
if not currency.start_date or not currency.end_date:
69+
raise HTTPException(
70+
status_code=status.HTTP_400_BAD_REQUEST,
71+
detail='The start_date and end_date fields are required.'
72+
)
73+
74+
if currency.start_date > currency.end_date:
75+
raise HTTPException(
76+
status_code=status.HTTP_400_BAD_REQUEST,
77+
detail='The start_date cannot be later than the end_date.'
78+
)
5879

5980
chart = await create_chart(currency, req.app.state.db)
6081

0 commit comments

Comments
 (0)