feat: add users chats and member access
This commit is contained in:
+35
-4
@@ -1,8 +1,11 @@
|
||||
"""HTTP routes; domain operations are added in subsequent units."""
|
||||
"""Lab 1 HTTP routes."""
|
||||
|
||||
from fastapi import APIRouter
|
||||
from uuid import UUID
|
||||
|
||||
from .models import CreateUser
|
||||
from fastapi import APIRouter, Depends, Header, HTTPException, Query
|
||||
|
||||
from .models import CreateChat, CreateUser
|
||||
from .store import store
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -19,4 +22,32 @@ def ready():
|
||||
|
||||
@router.post("/api/v1/users", status_code=201)
|
||||
def register_user(body: CreateUser):
|
||||
raise NotImplementedError("User registration is implemented in U2")
|
||||
return store.register(body.username, body.display_name)
|
||||
|
||||
|
||||
def current_user(x_user_id: str | None = Header(None, alias="X-User-Id")) -> dict:
|
||||
return store.user(x_user_id)
|
||||
|
||||
|
||||
@router.get("/api/v1/users/me")
|
||||
def user_me(actor: dict = Depends(current_user)):
|
||||
return actor
|
||||
|
||||
|
||||
@router.post("/api/v1/chats", status_code=201)
|
||||
def create_chat(body: CreateChat, actor: dict = Depends(current_user)):
|
||||
return store.create_chat(actor["id"], body.title, body.member_ids)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats")
|
||||
def list_chats(
|
||||
limit: str = Query("50"), cursor: str | None = Query(None, min_length=1), actor: dict = Depends(current_user)
|
||||
):
|
||||
if not limit.isdecimal() or not 1 <= int(limit) <= 100:
|
||||
raise HTTPException(400)
|
||||
return store.list_chats(actor["id"], int(limit), cursor)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}")
|
||||
def get_chat(chat_id: UUID, actor: dict = Depends(current_user)):
|
||||
return store.chat(actor["id"], str(chat_id))
|
||||
|
||||
Reference in New Issue
Block a user