Skip to content

Commit

Permalink
asset, loan history get api 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
onaries committed Dec 25, 2023
1 parent cc0d237 commit 247b720
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
46 changes: 46 additions & 0 deletions app/routes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import calendar
from collections import defaultdict
from datetime import timedelta
from typing import Optional, Union
from fastapi import Form, Query
Expand Down Expand Up @@ -32,6 +33,7 @@
StatementSchema,
StatementSummarySchema,
StatementCategorySumSchema,
AssetSchema2,
)
from .in_schema import (
MainCategoryIn,
Expand Down Expand Up @@ -333,13 +335,57 @@ async def delete_asset(id: int, db: Session = Depends(get_db)):
return {"message": "Asset deleted successfully"}


@router.get("/asset/{id}/history", response_model=list[AssetSchema2])
async def get_asset_detail_history(id: int, db: Session = Depends(get_db)):
asset = db.query(Asset).filter(Asset.id == id).first()

if asset is None:
raise HTTPException(status_code=404, detail="Asset not found")

data = defaultdict(int)
for history in asset.versions:
if history.updated_at is None:
data[history.created_at.strftime("%Y-%m-%d")] = history.amount

else:
data[history.updated_at.strftime("%Y-%m-%d")] = history.amount

result = []
for k, v in data.items():
result.append(dict(date=k, amount=v))

return result[-20:]


@router.get("/loan", response_model=Page[LoanSchema])
async def get_loans(
sort: str = "id", order: str = "ASC", db: Session = Depends(get_db)
):
return paginate(db, select(Loan).order_by(text(f"{sort} {order}")))


@router.get("/loan/{id}/history", response_model=list[AssetSchema2])
async def get_loan_detail_history(id: int, db: Session = Depends(get_db)):
loan = db.query(Loan).filter(Loan.id == id).first()

if loan is None:
raise HTTPException(status_code=404, detail="Loan not found")

data = defaultdict(int)
for history in loan.versions:
if history.updated_at is None:
data[history.created_at.strftime("%Y-%m-%d")] = history.amount

else:
data[history.updated_at.strftime("%Y-%m-%d")] = history.amount

result = []
for k, v in data.items():
result.append(dict(date=k, amount=v))

return result[-20:]


@router.post("/loan")
async def create_loan(
loan_in: LoanIn,
Expand Down
5 changes: 5 additions & 0 deletions app/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,8 @@ class StatementCategorySumSchema(BaseModel):
discount: int = 0
total: int = 0
total_no_discount: int = 0


class AssetSchema2(BaseModel):
date: str
amount: int

0 comments on commit 247b720

Please sign in to comment.