sessions/internal/service/session.go
Dawid Wysokiński 8e8e7e1f94
All checks were successful
continuous-integration/drone/push Build is passing
feat: add a new REST endpoint - PUT /api/v1/user/sessions/:server (#8)
Reviewed-on: #8
2022-11-24 05:17:32 +00:00

40 lines
1.1 KiB
Go

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)
}
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
}