Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add create_user endpoint #658

Merged
merged 9 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions margin_app/app/api/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This module contains the API routes for the user.
"""
from fastapi import APIRouter, HTTPException, status, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from ..database import get_db
from ..crud.user import user_crud as crud_create_user
from app.schemas.user import UserResponse, UserCreate

router = APIRouter()


@router.post(
"/users",
response_model=UserResponse,
status_code=status.HTTP_201_CREATED,
)

async def user_crud(user: UserCreate, db: AsyncSession = Depends(get_db))-> UserResponse:
"""
Create a new user.

Parameters:
- wallet_id: str, the wallet ID of the user

Returns:
- UserResponse: The created user object
"""
db_user = await crud_create_user(db, user.wallet_id)
if db_user is None:
raise HTTPException(status_code=400, detail="User could not be created")
return db_user
3 changes: 3 additions & 0 deletions margin_app/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
from loguru import logger

from app.api.pools import router as pool_router
from app.api.user import router as user_router

# Initialize FastAPI app
app = FastAPI()
app.include_router(pool_router, prefix="/api/pool", tags=["Pool"])

app.include_router(user_router, prefix="/api/user", tags=["User"])


# Configure Loguru
logger.remove() # Remove default logger to configure custom settings
Expand Down
32 changes: 32 additions & 0 deletions margin_app/app/schemas/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""
This module contains Pydantic schemas for User.
"""

from pydantic import BaseModel, ConfigDict
from uuid import UUID
from datetime import datetime

class UserBase(BaseModel):
"""
User base model
"""

id: int
wallet_id: str
created_at: datetime

class UserCreate(UserBase):
"""
User create model
"""

pass

class UserResponse(UserBase):
"""
User response model
"""

id: UUID

model_config = ConfigDict(from_attributes=True)