package service import ( "context" "errors" "fmt" "gitea.dwysokinski.me/twhelp/sessions/internal/domain" ) //counterfeiter:generate -o internal/mock/session_repository.gen.go . SessionRepository type SessionRepository interface { CreateOrUpdate(ctx context.Context, params domain.CreateSessionParams) (domain.Session, error) Get(ctx context.Context, userID int64, serverKey string) (domain.Session, error) } type Session struct { repo SessionRepository userSvc UserGetter } func NewSession(repo SessionRepository, userSvc UserGetter) *Session { return &Session{repo: repo, userSvc: userSvc} } func (s *Session) CreateOrUpdate(ctx context.Context, params domain.CreateSessionParams) (domain.Session, error) { if _, err := s.userSvc.Get(ctx, params.UserID()); err != nil { if errors.Is(err, domain.UserNotFoundError{ID: params.UserID()}) { return domain.Session{}, domain.UserDoesNotExistError{ID: params.UserID()} } return domain.Session{}, fmt.Errorf("UserService.Get: %w", err) } sess, err := s.repo.CreateOrUpdate(ctx, params) if err != nil { return domain.Session{}, fmt.Errorf("SessionRepository.CreateOrUpdate: %w", err) } return sess, nil } func (s *Session) Get(ctx context.Context, userID int64, serverKey string) (domain.Session, error) { sess, err := s.repo.Get(ctx, userID, serverKey) if err != nil { return domain.Session{}, fmt.Errorf("SessionRepository.Get: %w", err) } return sess, nil }