forked from mtusi2026/mtusi2026lab5
59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Body, Depends, Query, status
|
|
|
|
from application.api.dependencies import get_current_user
|
|
from domain import (
|
|
ChatPageSchema,
|
|
ChatSchema,
|
|
CreateChatSchema,
|
|
UserSchema,
|
|
)
|
|
from infrastructure.dependencies import ServiceCreator
|
|
from infrastructure.services import ChatService
|
|
|
|
chats_router = APIRouter(prefix="/chats", tags=["Chats"])
|
|
|
|
|
|
@chats_router.post(
|
|
"",
|
|
status_code=status.HTTP_201_CREATED,
|
|
response_model=ChatSchema,
|
|
)
|
|
async def create_chat(
|
|
body: Annotated[CreateChatSchema, Body(...)],
|
|
current_user: UserSchema = Depends(get_current_user),
|
|
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
|
) -> ChatSchema:
|
|
"""Вызывающий должен входить в member_ids"""
|
|
return await service.create_chat(current_user, body)
|
|
|
|
|
|
@chats_router.get(
|
|
"",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=ChatPageSchema,
|
|
)
|
|
async def list_chats(
|
|
limit: Annotated[int, Query(ge=1, le=100)] = 50,
|
|
cursor: Annotated[str | None, Query(min_length=1)] = None,
|
|
current_user: UserSchema = Depends(get_current_user),
|
|
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
|
) -> ChatPageSchema:
|
|
"""Чаты текущего пользователя по (created_at, id) по возрастанию"""
|
|
return await service.list_chats(current_user, limit, cursor)
|
|
|
|
|
|
@chats_router.get(
|
|
"/{chat_id}",
|
|
status_code=status.HTTP_200_OK,
|
|
response_model=ChatSchema,
|
|
)
|
|
async def get_chat(
|
|
chat_id: UUID,
|
|
current_user: UserSchema = Depends(get_current_user),
|
|
service: ChatService = Depends(ServiceCreator.get_chat_service),
|
|
) -> ChatSchema:
|
|
return await service.get_chat(current_user, chat_id)
|