|
6 | 6 | data, and returns a formatted chart response. |
7 | 7 | """ |
8 | 8 | from datetime import datetime |
| 9 | +from dateutil.relativedelta import relativedelta |
9 | 10 |
|
10 | 11 | from fastapi import APIRouter, status, Depends, HTTPException, Request |
11 | 12 | from fastapi.responses import StreamingResponse |
@@ -44,17 +45,37 @@ async def get_chart(req: Request, currency: Currency = Depends()) -> StreamingRe |
44 | 45 | detail='The from_currency and conv_currency fields are required.' |
45 | 46 | ) |
46 | 47 |
|
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 |
52 | 49 |
|
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 |
57 | 66 | ) |
| 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 | + ) |
58 | 79 |
|
59 | 80 | chart = await create_chart(currency, req.app.state.db) |
60 | 81 |
|
|
0 commit comments