на оценку 3

This commit is contained in:
2026-10-21 13:51:35 +03:00
parent 3f69970b05
commit bd28f1ae26
24 changed files with 1130 additions and 91 deletions
+26 -1
View File
@@ -2,16 +2,41 @@ package application
import (
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"mtusi2026lab3/internal/domain"
)
type canonicalMessageRequest struct {
Text string `json:"text"`
AttachmentIDs []string `json:"attachment_ids"`
}
func (s *Service) SendMessage(ctx context.Context, command SendMessageCommand) (domain.Message, error) {
if err := domain.ValidateMessageText(command.Text); err != nil {
return domain.Message{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
}
return s.repository.CreateMessage(ctx, command.ChatID, command.ActorID, command.Text)
canonicalBody, err := json.Marshal(canonicalMessageRequest{
Text: command.Text,
AttachmentIDs: make([]string, 0),
})
if err != nil {
return domain.Message{}, fmt.Errorf("canonicalize message request: %w", err)
}
return s.repository.CreateMessageIdempotently(ctx, IdempotentMessageWrite{
ActorID: command.ActorID,
ChatID: command.ChatID,
Text: command.Text,
Method: command.Method,
CanonicalPath: command.CanonicalPath,
Key: command.IdempotencyKey,
RequestHash: sha256.Sum256(canonicalBody),
TTL: s.idempotencyTTL,
})
}
func (s *Service) ListMessages(