добавлены чаты, сообщения и проверка сохранности

This commit is contained in:
2026-10-08 00:24:34 +03:00
parent 7f8bcfd234
commit 4922e68dca
3 changed files with 599 additions and 5 deletions
+134 -4
View File
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta, timezone
from uuid import uuid4
from uuid import UUID, uuid4
from fastapi import Depends, FastAPI, Request
from fastapi.exceptions import RequestValidationError
@@ -16,11 +16,16 @@ from starlette.exceptions import HTTPException as StarletteHTTPException
from src.database import get_db_session
from src.errors import APIError
from src.models import CreateUser, SessionResponse, User
from src.models import CreateUser, SessionResponse, User, CreateChat, Chat, ChatPage, CreateMessage, Message, MessagePage
from src.repositories import (
create_chat,
list_chats,
create_user as create_user_in_db,
get_user_by_id,
get_chat_by_id,
get_user_credentials_by_username,
create_message,
get_messages,
)
from src.security import verify_password
from src.sessions import (
@@ -286,7 +291,7 @@ async def create_auth_session(
return SessionResponse(
token=token,
token_type="bearer",
token_type="Bearer",
expires_at=expires_at,
user=user,
)
@@ -300,4 +305,129 @@ async def delete_current_session(
credentials: HTTPAuthorizationCredentials | None = Depends(bearer_security),
current_user: User = Depends(get_current_user),
):
await delete_session(credentials.credentials)
await delete_session(credentials.credentials)
@app.post(
"/api/v1/chats",
response_model=Chat,
status_code=201,
)
async def create_chat_endpoint(
data: CreateChat,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db_session),
) -> Chat:
try:
chat = await create_chat(
session=session,
data=data,
current_user_id=current_user.id,
)
await session.commit()
except Exception:
await session.rollback()
raise
return chat
@app.get(
"/api/v1/chats",
response_model=ChatPage,
)
async def get_chats(
limit: int = 50,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db_session),
) -> ChatPage:
if limit < 1 or limit > 100:
raise APIError(
status_code=400,
code="invalid_request",
message="Limit must be between 1 and 100",
)
return await list_chats(
session=session,
current_user_id=current_user.id,
limit=limit,
)
@app.get(
"/api/v1/chats/{chat_id}",
response_model=Chat,
)
async def get_chat_endpoint(
chat_id: UUID,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db_session),
) -> Chat:
chat = await get_chat_by_id(
session=session,
chat_id=chat_id,
current_user_id=current_user.id,
)
if chat is None:
raise APIError(
status_code=404,
code="not_found",
message="Chat not found",
)
return chat
@app.post(
"/api/v1/chats/{chat_id}/messages",
response_model=Message,
status_code=201,
)
async def create_message_endpoint(
chat_id: UUID,
data: CreateMessage,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db_session),
) -> Message:
try:
message = await create_message(
session=session,
chat_id=chat_id,
current_user_id=current_user.id,
data=data,
)
await session.commit()
except Exception:
await session.rollback()
raise
return message
@app.get(
"/api/v1/chats/{chat_id}/messages",
response_model=MessagePage,
)
async def get_messages_endpoint(
chat_id: UUID,
limit: int = 50,
cursor: str | None = None,
current_user: User = Depends(get_current_user),
session: AsyncSession = Depends(get_db_session),
) -> MessagePage:
if limit < 1 or limit > 100:
raise APIError(
status_code=400,
code="invalid_request",
message="Limit must be between 1 and 100",
)
return await get_messages(
session=session,
chat_id=chat_id,
current_user_id=current_user.id,
limit=limit,
cursor=cursor,
)