Files
mtusi2026lab2/internal/user/service.go
T

56 lines
1.3 KiB
Go

package user
import (
"context"
"fmt"
"github.com/google/uuid"
"messenger/internal/platform/ident"
)
type Service struct {
repo Repository
}
func NewService(repo Repository) *Service {
return &Service{repo: repo}
}
func (s *Service) Register(ctx context.Context, username, displayName string) (User, error) {
u, err := New(ident.New(), username, displayName)
if err != nil {
return User{}, err
}
if err := s.repo.Create(ctx, u); err != nil {
return User{}, err
}
return u, nil
}
func (s *Service) GetByID(ctx context.Context, id uuid.UUID) (User, error) {
return s.repo.GetByID(ctx, id)
}
// Exists serves the identification middleware; it is the single-id form of
// ExistingIDs so existence is answered by one code path.
func (s *Service) Exists(ctx context.Context, id uuid.UUID) (bool, error) {
found, err := s.ExistingIDs(ctx, []uuid.UUID{id})
if err != nil {
return false, err
}
return len(found) == 1, nil
}
// ExistingIDs serves the chats module when it validates member lists.
func (s *Service) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
if len(ids) == 0 {
return []uuid.UUID{}, nil
}
found, err := s.repo.ExistingIDs(ctx, ids)
if err != nil {
return nil, fmt.Errorf("user lookup: %w", err)
}
return found, nil
}