go-cook/api/services/users.go

44 lines
936 B
Go

package services
import (
"database/sql"
"errors"
"go-cook/api/models"
"go-cook/api/repositories"
)
const (
ErrPasswordNotLongEnough = "password needs to be 8 character or longer"
)
// This will handle operations that are user related, but one layer higher then the repository
type UserService struct {
repo repositories.UserRepository
}
func NewUserService(conn *sql.DB) UserService {
return UserService{
repo: repositories.NewUserRepository(conn),
}
}
func (us UserService) DoesUserExist(username string) error {
_, err := us.repo.GetByName(username)
if err != nil {
return err
}
return nil
}
func (us UserService) CreateNewUser(name, password string) (models.UserModel, error) {
us.repo.NewUser(name, password)
return models.UserModel{}, nil
}
func (us UserService) CheckPasswordForRequirements(password string) error {
if len(password) <= 8 {
return errors.New(ErrPasswordNotLongEnough)
}
return nil
}