forked from mtusi2026/mtusi2026lab3
feat: API replicas over HAProxy (3)
This commit is contained in:
@@ -2,6 +2,9 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"uuid"
|
||||
|
||||
@@ -17,7 +20,7 @@ func NewMessageService(chats *ChatService, messages MessageRepository) *MessageS
|
||||
return &MessageService{chats: chats, messages: messages}
|
||||
}
|
||||
|
||||
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, text string) (domain.Message, error) {
|
||||
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string) (domain.Message, error) {
|
||||
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
@@ -31,10 +34,12 @@ func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uui
|
||||
Text: text,
|
||||
CreatedAt: now(),
|
||||
}
|
||||
if err := s.messages.CreateMessage(ctx, m); err != nil {
|
||||
return domain.Message{}, err
|
||||
sum := sha256.Sum256([]byte(text))
|
||||
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, hex.EncodeToString(sum[:]))
|
||||
if errors.Is(err, domain.ErrConflict) {
|
||||
return domain.Message{}, fmt.Errorf("%w: Idempotency-Key was already used with a different body", domain.ErrConflict)
|
||||
}
|
||||
return m, nil
|
||||
return stored, err
|
||||
}
|
||||
|
||||
func (s *MessageService) History(ctx context.Context, actor domain.User, chatID uuid.UUID, after *domain.Cursor, limit int) (domain.Page[domain.Message], error) {
|
||||
|
||||
Reference in New Issue
Block a user