139 lines
3.1 KiB
Go
139 lines
3.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/charawein/mtusi2026lab2/internal/domain"
|
|
"github.com/charawein/mtusi2026lab2/internal/repository"
|
|
)
|
|
|
|
type ChatService struct {
|
|
chats repository.ChatRepository
|
|
users repository.UserRepository
|
|
}
|
|
|
|
func NewChatService(chats repository.ChatRepository, users repository.UserRepository) *ChatService {
|
|
return &ChatService{chats: chats, users: users}
|
|
}
|
|
|
|
func (s *ChatService) Create(ctx context.Context, callerID, title string, memberIDs []string) (*domain.Chat, error) {
|
|
if !validateTitle(title) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
|
|
// Нормализация UUID: убираем разницу регистра.
|
|
normalized := make([]string, 0, len(memberIDs))
|
|
seen := make(map[string]struct{}, len(memberIDs))
|
|
callerFound := false
|
|
callerParsed, err := uuid.Parse(callerID)
|
|
if err != nil {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
callerID = callerParsed.String()
|
|
|
|
for _, id := range memberIDs {
|
|
parsed, err := uuid.Parse(id)
|
|
if err != nil {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
id = parsed.String()
|
|
if _, dup := seen[id]; dup {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
seen[id] = struct{}{}
|
|
normalized = append(normalized, id)
|
|
if id == callerID {
|
|
callerFound = true
|
|
}
|
|
}
|
|
if !callerFound {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
|
|
for id := range seen {
|
|
if _, err := s.users.GetByID(ctx, id); err != nil {
|
|
if errors.Is(err, repository.ErrNotFound) {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
c := &domain.Chat{
|
|
ID: newID(),
|
|
Title: title,
|
|
MemberIDs: normalized,
|
|
CreatedAt: nowUTC(),
|
|
}
|
|
if err := s.chats.Create(ctx, c); err != nil {
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (s *ChatService) Get(ctx context.Context, callerID, chatID string) (*domain.Chat, error) {
|
|
parsed, err := uuid.Parse(chatID)
|
|
if err != nil {
|
|
return nil, ErrInvalidInput
|
|
}
|
|
chatID = parsed.String()
|
|
|
|
ok, err := s.chats.IsMember(ctx, chatID, callerID)
|
|
if err != nil {
|
|
if errors.Is(err, repository.ErrNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
if !ok {
|
|
return nil, ErrNotFound
|
|
}
|
|
c, err := s.chats.GetByID(ctx, chatID)
|
|
if err != nil {
|
|
if errors.Is(err, repository.ErrNotFound) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func (s *ChatService) List(ctx context.Context, callerID, cursor string, limit int) ([]*domain.Chat, string, error) {
|
|
if limit < 1 || limit > 100 {
|
|
return nil, "", ErrInvalidInput
|
|
}
|
|
|
|
cursorTime := time.Unix(0, 0).UTC()
|
|
cursorID := "00000000-0000-0000-0000-000000000000"
|
|
if cursor != "" {
|
|
t, id, err := decodeCursor(cursor, callerID)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
cursorTime, cursorID = t, id
|
|
}
|
|
|
|
items, err := s.chats.ListByMember(ctx, callerID, cursorTime, cursorID, limit+1)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
if items == nil {
|
|
items = []*domain.Chat{}
|
|
}
|
|
|
|
var next string
|
|
if len(items) > limit {
|
|
items = items[:limit]
|
|
last := items[limit-1]
|
|
next = encodeCursor(callerID, last.CreatedAt, last.ID)
|
|
}
|
|
return items, next, nil
|
|
}
|