Bring the application, own tests, Dockerfile, toolchain, compose stack, migrations, secret generator, restart hook, evidence scripts and CI job from the lab 2 solution commit 8cddb09 unchanged, merge the lab 2 Makefile targets into the lab 3 skeleton Makefile without touching its targets, and append the lab 2 solution variables to the skeleton .env.example. The package version follows the contract version 1.3.0. This is the starting point of the lab 3 cycle: lint, own tests and the offline skeleton check pass before any lab 3 change. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
80 lines
2.2 KiB
Python
80 lines
2.2 KiB
Python
from typing import Annotated
|
|
from uuid import UUID
|
|
|
|
from dishka.integrations.fastapi import FromDishka, inject
|
|
from fastapi import APIRouter, Query, status
|
|
|
|
from messenger.messaging.application.bus import MessagingCommandBus
|
|
from messenger.messaging.application.commands import CreateChat, PostMessage
|
|
from messenger.messaging.application.queries import GetChat, ListChats, ReadHistory
|
|
from messenger.messaging.entrypoints.schemas import (
|
|
ChatOut,
|
|
CreateChatRequest,
|
|
CreateMessageRequest,
|
|
MessageOut,
|
|
PageOut,
|
|
PageQuery,
|
|
)
|
|
from messenger.shared.entrypoints.actor import Actor
|
|
|
|
chats_router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
|
|
messages_router = APIRouter(prefix="/api/v1/chats/{chat_id}/messages", tags=["messages"])
|
|
|
|
|
|
@chats_router.post("", status_code=status.HTTP_201_CREATED)
|
|
@inject
|
|
async def create_chat(
|
|
actor: Actor,
|
|
body: CreateChatRequest,
|
|
bus: FromDishka[MessagingCommandBus],
|
|
) -> ChatOut:
|
|
chat = await bus.execute(
|
|
CreateChat(creator_id=actor, title=body.title, member_ids=body.member_ids)
|
|
)
|
|
return ChatOut.of(chat)
|
|
|
|
|
|
@chats_router.get("")
|
|
@inject
|
|
async def list_chats(
|
|
actor: Actor,
|
|
page: Annotated[PageQuery, Query()],
|
|
query: FromDishka[ListChats],
|
|
) -> PageOut[ChatOut]:
|
|
chats = await query(actor, limit=page.limit, cursor=page.cursor)
|
|
return PageOut[ChatOut].of(chats, ChatOut.of)
|
|
|
|
|
|
@chats_router.get("/{chat_id}")
|
|
@inject
|
|
async def read_chat(
|
|
chat_id: UUID,
|
|
actor: Actor,
|
|
query: FromDishka[GetChat],
|
|
) -> ChatOut:
|
|
return ChatOut.of(await query(chat_id, actor))
|
|
|
|
|
|
@messages_router.post("", status_code=status.HTTP_201_CREATED)
|
|
@inject
|
|
async def post_message(
|
|
chat_id: UUID,
|
|
actor: Actor,
|
|
body: CreateMessageRequest,
|
|
bus: FromDishka[MessagingCommandBus],
|
|
) -> MessageOut:
|
|
message = await bus.execute(PostMessage(sender_id=actor, chat_id=chat_id, text=body.text))
|
|
return MessageOut.of(message)
|
|
|
|
|
|
@messages_router.get("")
|
|
@inject
|
|
async def read_history(
|
|
chat_id: UUID,
|
|
actor: Actor,
|
|
page: Annotated[PageQuery, Query()],
|
|
query: FromDishka[ReadHistory],
|
|
) -> PageOut[MessageOut]:
|
|
messages = await query(chat_id, actor, limit=page.limit, cursor=page.cursor)
|
|
return PageOut[MessageOut].of(messages, MessageOut.of)
|