forked from mtusi2026/mtusi2026lab4
50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, Query
|
|
|
|
from ...domain.models import User
|
|
from ...services.chats import ChatService
|
|
from .. import cursors
|
|
from ..deps import chat_service, current_user
|
|
from ..schemas import (
|
|
LIMIT_DEFAULT,
|
|
LIMIT_MAX,
|
|
LIMIT_MIN,
|
|
ChatOut,
|
|
ChatPageOut,
|
|
CreateChat,
|
|
)
|
|
|
|
router = APIRouter(prefix='/api/v1/chats', tags=['chats'])
|
|
|
|
|
|
@router.post('', status_code=201, response_model=ChatOut)
|
|
async def create(
|
|
body: CreateChat,
|
|
actor: User = Depends(current_user),
|
|
chats: ChatService = Depends(chat_service),
|
|
) -> ChatOut:
|
|
return ChatOut.from_domain(await chats.create(actor, body.title, body.member_ids))
|
|
|
|
|
|
@router.get('', response_model=ChatPageOut)
|
|
async def page(
|
|
actor: User = Depends(current_user),
|
|
chats: ChatService = Depends(chat_service),
|
|
limit: int = Query(LIMIT_DEFAULT, ge=LIMIT_MIN, le=LIMIT_MAX),
|
|
cursor: Optional[str] = Query(None, min_length=1, max_length=cursors.MAX_LENGTH),
|
|
) -> ChatPageOut:
|
|
scope = f'chats:{actor.id}'
|
|
after = cursors.decode(scope, cursor) if cursor is not None else None
|
|
return ChatPageOut.from_domain(await chats.page(actor, after, limit), scope)
|
|
|
|
|
|
@router.get('/{chat_id}', response_model=ChatOut)
|
|
async def get(
|
|
chat_id: UUID,
|
|
actor: User = Depends(current_user),
|
|
chats: ChatService = Depends(chat_service),
|
|
) -> ChatOut:
|
|
return ChatOut.from_domain(await chats.get(actor, chat_id))
|