Spaces:
Sleeping
Sleeping
Praneeth Yerrapragada commited on
Commit ·
114f9f7
1
Parent(s): d4e18c8
fix: fix transaction api responses
Browse files- app/api/routers/transaction.py +6 -5
- app/model/transaction.py +7 -5
- app/schema/index.py +2 -2
app/api/routers/transaction.py
CHANGED
|
@@ -2,7 +2,7 @@ from typing import List
|
|
| 2 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 3 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 4 |
from app.model.transaction import Transaction as TransactionModel
|
| 5 |
-
from app.schema.index import
|
| 6 |
from app.engine.postgresdb import get_db_session
|
| 7 |
|
| 8 |
transaction_router = r = APIRouter(prefix="/api/v1/transactions", tags=["transactions"])
|
|
@@ -10,7 +10,7 @@ transaction_router = r = APIRouter(prefix="/api/v1/transactions", tags=["transac
|
|
| 10 |
|
| 11 |
@r.get(
|
| 12 |
"/{user_id}",
|
| 13 |
-
response_model=List[
|
| 14 |
responses={
|
| 15 |
200: {"description": "New user created"},
|
| 16 |
400: {"description": "Bad request"},
|
|
@@ -23,6 +23,7 @@ async def get_transactions(user_id: int, db: AsyncSession = Depends(get_db_sessi
|
|
| 23 |
Retrieve all transactions.
|
| 24 |
"""
|
| 25 |
result = await TransactionModel.get_by_user(db, user_id)
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
| 2 |
from fastapi import APIRouter, Depends, HTTPException, status
|
| 3 |
from sqlalchemy.ext.asyncio import AsyncSession
|
| 4 |
from app.model.transaction import Transaction as TransactionModel
|
| 5 |
+
from app.schema.index import TransactionResponse
|
| 6 |
from app.engine.postgresdb import get_db_session
|
| 7 |
|
| 8 |
transaction_router = r = APIRouter(prefix="/api/v1/transactions", tags=["transactions"])
|
|
|
|
| 10 |
|
| 11 |
@r.get(
|
| 12 |
"/{user_id}",
|
| 13 |
+
response_model=List[TransactionResponse],
|
| 14 |
responses={
|
| 15 |
200: {"description": "New user created"},
|
| 16 |
400: {"description": "Bad request"},
|
|
|
|
| 23 |
Retrieve all transactions.
|
| 24 |
"""
|
| 25 |
result = await TransactionModel.get_by_user(db, user_id)
|
| 26 |
+
all_rows = result.all()
|
| 27 |
+
if len(all_rows) == 0:
|
| 28 |
+
raise HTTPException(status_code=status.HTTP_204_NO_CONTENT, detail="No transactions found for this user")
|
| 29 |
+
return all_rows
|
app/model/transaction.py
CHANGED
|
@@ -23,9 +23,10 @@ class Transaction(Base, BaseModel):
|
|
| 23 |
@classmethod
|
| 24 |
async def create(cls: "type[Transaction]", db: AsyncSession, **kwargs) -> "Transaction":
|
| 25 |
query = sql.insert(cls).values(**kwargs).returning(cls.id)
|
| 26 |
-
transactions = await db.
|
|
|
|
| 27 |
await db.commit()
|
| 28 |
-
return
|
| 29 |
|
| 30 |
@classmethod
|
| 31 |
async def update(cls: "type[Transaction]", db: AsyncSession, id: int, **kwargs) -> "Transaction":
|
|
@@ -36,12 +37,13 @@ class Transaction(Base, BaseModel):
|
|
| 36 |
.execution_options(synchronize_session="fetch")
|
| 37 |
.returning(cls.id)
|
| 38 |
)
|
| 39 |
-
transactions = await db.
|
|
|
|
| 40 |
await db.commit()
|
| 41 |
-
return
|
| 42 |
|
| 43 |
@classmethod
|
| 44 |
async def get_by_user(cls: "type[Transaction]", db: AsyncSession, user_id: int) -> "List[Transaction]":
|
| 45 |
query = sql.select(cls).where(cls.user_id == user_id)
|
| 46 |
-
transactions = await db.
|
| 47 |
return transactions
|
|
|
|
| 23 |
@classmethod
|
| 24 |
async def create(cls: "type[Transaction]", db: AsyncSession, **kwargs) -> "Transaction":
|
| 25 |
query = sql.insert(cls).values(**kwargs).returning(cls.id)
|
| 26 |
+
transactions = await db.scalars(query)
|
| 27 |
+
transaction = transactions.first()
|
| 28 |
await db.commit()
|
| 29 |
+
return transaction
|
| 30 |
|
| 31 |
@classmethod
|
| 32 |
async def update(cls: "type[Transaction]", db: AsyncSession, id: int, **kwargs) -> "Transaction":
|
|
|
|
| 37 |
.execution_options(synchronize_session="fetch")
|
| 38 |
.returning(cls.id)
|
| 39 |
)
|
| 40 |
+
transactions = await db.scalars(query)
|
| 41 |
+
transaction = transactions.first()
|
| 42 |
await db.commit()
|
| 43 |
+
return transaction
|
| 44 |
|
| 45 |
@classmethod
|
| 46 |
async def get_by_user(cls: "type[Transaction]", db: AsyncSession, user_id: int) -> "List[Transaction]":
|
| 47 |
query = sql.select(cls).where(cls.user_id == user_id)
|
| 48 |
+
transactions = await db.scalars(query)
|
| 49 |
return transactions
|
app/schema/index.py
CHANGED
|
@@ -37,7 +37,7 @@ class User(BaseModel):
|
|
| 37 |
transactions: "List[Transaction]" = []
|
| 38 |
|
| 39 |
|
| 40 |
-
class
|
| 41 |
transaction_date: datetime
|
| 42 |
category: str
|
| 43 |
name_description: str
|
|
@@ -45,5 +45,5 @@ class TransactionOutput(BaseModel):
|
|
| 45 |
type: TransactionType
|
| 46 |
|
| 47 |
|
| 48 |
-
class Transaction(
|
| 49 |
user: User
|
|
|
|
| 37 |
transactions: "List[Transaction]" = []
|
| 38 |
|
| 39 |
|
| 40 |
+
class TransactionResponse(PydanticBaseModel):
|
| 41 |
transaction_date: datetime
|
| 42 |
category: str
|
| 43 |
name_description: str
|
|
|
|
| 45 |
type: TransactionType
|
| 46 |
|
| 47 |
|
| 48 |
+
class Transaction(TransactionResponse):
|
| 49 |
user: User
|