serverctl/pkg/users/service.go

45 lines
1.0 KiB
Go
Raw Normal View History

2022-02-13 21:45:27 +01:00
package users
import (
"context"
"github.com/eko/gocache/cache"
"go.uber.org/zap"
)
type Service struct {
logger *zap.Logger
cache *cache.Cache
repository Repository
passwordHasher PasswordHasher
}
func NewService(l *zap.Logger, ur Repository, c *cache.Cache) *Service {
return &Service{
logger: l,
repository: ur,
cache: c,
passwordHasher: NewPlainTextPasswordHasher(),
}
}
func (s *Service) Create(email string, password string) (int, error) {
createUser, err := NewCreateUser(email, password, s.passwordHasher)
if err != nil {
return -1, err
}
var userId int
userId, err = s.repository.Create(context.Background(), createUser)
if err != nil {
s.logger.Warn("Could not create user in service")
return 0, err
}
return userId, nil
}
func (s *Service) Authenticate(ctx context.Context, email string, password string) (*User, error) {
user, err := s.repository.GetByEmail(ctx, email, s.passwordHasher.HashPassword(password))
return user, err
}