ЛР 2: мессенджер на PostgreSQL и сессии в Redis
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
|
||||
from ...domain.models import User
|
||||
from ...services.messages import MessageService
|
||||
from .. import cursors
|
||||
from ..deps import current_user, message_service
|
||||
from ..schemas import (
|
||||
LIMIT_DEFAULT,
|
||||
LIMIT_MAX,
|
||||
LIMIT_MIN,
|
||||
CreateMessage,
|
||||
MessageOut,
|
||||
MessagePageOut,
|
||||
)
|
||||
|
||||
router = APIRouter(prefix='/api/v1/chats/{chat_id}/messages', tags=['messages'])
|
||||
|
||||
|
||||
@router.post('', status_code=201, response_model=MessageOut)
|
||||
async def send(
|
||||
chat_id: UUID,
|
||||
body: CreateMessage,
|
||||
actor: User = Depends(current_user),
|
||||
messages: MessageService = Depends(message_service),
|
||||
) -> MessageOut:
|
||||
return MessageOut.from_domain(await messages.send(actor, chat_id, body.text))
|
||||
|
||||
|
||||
@router.get('', response_model=MessagePageOut)
|
||||
async def page(
|
||||
chat_id: UUID,
|
||||
actor: User = Depends(current_user),
|
||||
messages: MessageService = Depends(message_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),
|
||||
) -> MessagePageOut:
|
||||
scope = f'messages:{chat_id}'
|
||||
after = cursors.decode(scope, cursor) if cursor is not None else None
|
||||
return MessagePageOut.from_domain(await messages.history(actor, chat_id, after, limit), scope)
|
||||
Reference in New Issue
Block a user