45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package service
|
|
|
|
import (
|
|
"authorization/internal"
|
|
"authorization/internal/repository"
|
|
)
|
|
|
|
type UserService interface {
|
|
CreateUser(internal.User) (int, error)
|
|
ChangeUserRole(username string, Role string) (string, error)
|
|
GetUser(username string, hashedPassword string) (*internal.User, error)
|
|
}
|
|
|
|
type UserServiceImpl struct {
|
|
repo repository.UserResository
|
|
}
|
|
|
|
func newUserService(repo repository.UserResository) *UserServiceImpl {
|
|
return &UserServiceImpl{repo: repo}
|
|
}
|
|
|
|
func (s *UserServiceImpl) CreateUser(user internal.User) (int, error) {
|
|
return s.repo.CreateUser(user)
|
|
}
|
|
|
|
func (s *UserServiceImpl) ChangeUserRole(username string, userRole string) (string, error) {
|
|
newRole, err := internal.FromString(userRole)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
user, err := s.repo.UpdateUserRole(username, newRole)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return user, nil
|
|
}
|
|
|
|
func (s *UserServiceImpl) GetUser(username string, hashedPassword string) (*internal.User, error) {
|
|
user, err := s.repo.GetUser(username, hashedPassword)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|