File size: 1,605 Bytes
e92d5c0
51e559a
3ca3e6a
e92d5c0
 
3ca3e6a
 
e92d5c0
 
 
0b85fad
 
e92d5c0
0b85fad
e92d5c0
 
 
 
3ca3e6a
e92d5c0
 
 
 
 
 
 
 
3ca3e6a
e92d5c0
 
 
 
 
 
 
 
 
 
3ca3e6a
e92d5c0
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from sqlmodel import select

from .app_factory import OptionalOAuth, RequiredOAuth, create_app, get_session
from .schemas import UserCount

# Configure FastAPI app + database
app = create_app()


# Health check endpoint
@app.get("/api/health")
async def health():
    """Health check endpoint."""
    return {"status": "ok"}


# User endpoints
@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