56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package application
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"mtusi2026lab3/internal/domain"
|
|
)
|
|
|
|
func (s *Service) CreateChat(ctx context.Context, command CreateChatCommand) (domain.Chat, error) {
|
|
if err := domain.ValidateChatInput(command.Title, command.MemberIDs, command.ActorID); err != nil {
|
|
return domain.Chat{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
|
}
|
|
return s.repository.CreateChat(ctx, command.Title, command.MemberIDs)
|
|
}
|
|
|
|
func (s *Service) GetChat(ctx context.Context, actorID, chatID string) (domain.Chat, error) {
|
|
return s.repository.GetChatByID(ctx, chatID, actorID)
|
|
}
|
|
|
|
func (s *Service) ListChats(
|
|
ctx context.Context,
|
|
actorID string,
|
|
limit int,
|
|
cursorToken string,
|
|
) (domain.ChatPage, error) {
|
|
var cursor *domain.ChatCursor
|
|
if cursorToken != "" {
|
|
decoded, err := s.cursors.DecodeChat(cursorToken, actorID)
|
|
if err != nil {
|
|
return domain.ChatPage{}, ErrInvalidCursor
|
|
}
|
|
cursor = decoded
|
|
}
|
|
|
|
items, hasMore, err := s.repository.GetChatsForUser(ctx, actorID, limit, cursor)
|
|
if err != nil {
|
|
return domain.ChatPage{}, err
|
|
}
|
|
page := domain.ChatPage{Items: items}
|
|
if page.Items == nil {
|
|
page.Items = make([]domain.Chat, 0)
|
|
}
|
|
if hasMore {
|
|
last := page.Items[len(page.Items)-1]
|
|
next, encodeErr := s.cursors.EncodeChat(domain.ChatCursor{
|
|
CreatedAt: last.CreatedAt, ChatID: last.ID, UserID: actorID,
|
|
})
|
|
if encodeErr != nil {
|
|
return domain.ChatPage{}, encodeErr
|
|
}
|
|
page.NextCursor = &next
|
|
}
|
|
return page, nil
|
|
}
|