lab1
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/domain"
|
||||
)
|
||||
|
||||
//go:generate go tool mockery
|
||||
|
||||
type Repository interface {
|
||||
Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error)
|
||||
GetVisible(ctx context.Context, chatID, callerID uuid.UUID) (domain.Chat, error)
|
||||
ListVisible(
|
||||
ctx context.Context,
|
||||
callerID uuid.UUID,
|
||||
after *domain.Cursor,
|
||||
limit int,
|
||||
) (domain.Page[domain.Chat], error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
chats Repository
|
||||
}
|
||||
|
||||
func NewService(chats Repository) *Service {
|
||||
return &Service{chats: chats}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, draft domain.ChatDraft) (domain.Chat, error) {
|
||||
if err := draft.Validate(); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
created, err := s.chats.Create(ctx, draft)
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("create chat: %w", err)
|
||||
}
|
||||
|
||||
return created, nil
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, callerID, chatID uuid.UUID) (domain.Chat, error) {
|
||||
found, err := s.chats.GetVisible(ctx, chatID, callerID)
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("get chat: %w", err)
|
||||
}
|
||||
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (s *Service) List(
|
||||
ctx context.Context,
|
||||
callerID uuid.UUID,
|
||||
after *domain.Cursor,
|
||||
limit int,
|
||||
) (domain.Page[domain.Chat], error) {
|
||||
if after != nil && !after.BelongsTo(callerID) {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("cursor is bound to another list: %w", domain.ErrInvalidCursor)
|
||||
}
|
||||
|
||||
page, err := s.chats.ListVisible(ctx, callerID, after, limit)
|
||||
if err != nil {
|
||||
return domain.Page[domain.Chat]{}, fmt.Errorf("list chats: %w", err)
|
||||
}
|
||||
|
||||
return page, nil
|
||||
}
|
||||
Reference in New Issue
Block a user