|
from sqlmodel import select |
|
|
|
from .app_factory import OptionalOAuth, RequiredOAuth, create_app, get_session |
|
from .schemas import UserCount |
|
|
|
|
|
app = create_app() |
|
|
|
|
|
|
|
@app.get("/api/health") |
|
async def health(): |
|
"""Health check endpoint.""" |
|
return {"status": "ok"} |
|
|
|
|
|
|
|
@app.get("/api/user") |
|
async def get_user(oauth_info: OptionalOAuth): |
|
"""Get user information.""" |
|
return { |
|
"connected": oauth_info is not None, |
|
"username": oauth_info.user_info.preferred_username if oauth_info else None, |
|
} |
|
|
|
|
|
@app.get("/api/user/count") |
|
async def get_user_count(oauth_info: RequiredOAuth) -> UserCount: |
|
"""Get user count.""" |
|
with get_session() as session: |
|
statement = select(UserCount).where(UserCount.name == oauth_info.user_info.name) |
|
user_count = session.exec(statement).first() |
|
if user_count is None: |
|
user_count = UserCount(name=oauth_info.user_info.name, count=0) |
|
return user_count |
|
|
|
|
|
@app.post("/api/user/count/increment") |
|
async def increment_user_count(oauth_info: RequiredOAuth) -> UserCount: |
|
"""Increment user count.""" |
|
with get_session() as session: |
|
statement = select(UserCount).where(UserCount.name == oauth_info.user_info.name) |
|
user_count = session.exec(statement).first() |
|
if user_count is None: |
|
user_count = UserCount(name=oauth_info.user_info.name, count=0) |
|
|
|
user_count.count += 1 |
|
session.add(user_count) |
|
session.commit() |
|
session.refresh(user_count) |
|
return user_count |
|
|