arimelody-web/service/account/account.go

130 lines
4.5 KiB
Go

package account
import (
"arimelody-web/errors"
"arimelody-web/model"
repository "arimelody-web/repository/account"
"arimelody-web/service/validator"
"log"
)
type AccountService struct {
repo repository.AccountRepository
log *log.Logger
}
func NewAccountService(repo repository.AccountRepository, logger *log.Logger) (*AccountService) {
return &AccountService{
repo: repo,
log: logger,
}
}
func (s *AccountService) GetAll() ([]*model.Account, error) {
return s.repo.GetAll()
}
func (s *AccountService) GetCount() (int, error) {
return s.repo.GetCount()
}
func (s *AccountService) GetByID(id string) (*model.Account, error) {
account, err := s.repo.GetByID(id)
if err != nil { return nil, err }
if account == nil { return nil, errors.NewNotExistError("Account does not exist") }
return account, nil
}
func (s *AccountService) GetByUsername(username string) (*model.Account, error) {
account, err := s.repo.GetByUsername(username)
if err != nil { return nil, err }
if account == nil { return nil, errors.NewNotExistError("Account does not exist") }
return account, nil
}
func (s *AccountService) GetByEmail(email string) (*model.Account, error) {
account, err := s.repo.GetByEmail(email)
if err != nil { return nil, err }
if account == nil { return nil, errors.NewNotExistError("Account does not exist") }
return account, nil
}
func (s *AccountService) Create(
username string,
password string,
email *string,
avatarURL *string,
) (string, error) {
if len(username) == 0 { return "", errors.NewValidationError("Username cannot be empty") }
if !validator.ValidateID(username) { return "", errors.NewValidationError("Username contains invalid characters") }
if len(password) == 0 { return "", errors.NewValidationError("Password cannot be empty") }
if email != nil && len(*email) == 0 { return "", errors.NewValidationError("Email cannot be empty") }
var id string
var err error
if id, err = s.repo.Create(username, password, email, avatarURL); err != nil {
return id, err
}
s.log.Printf("Created account '%s' (%s)", username, id)
return id, nil
}
func (s *AccountService) ChangeUsername(id string, username string) error {
if len(username) == 0 { return errors.NewValidationError("Username cannot be empty") }
if !validator.ValidateID(username) { return errors.NewValidationError("Username contains invalid characters") }
if err := s.repo.UpdateUsername(id, username); err != nil { return err }
s.log.Printf("Changed username for %s to '%s'", id, username)
return nil
}
func (s *AccountService) ChangePassword(id string, password string) error {
if len(password) == 0 { return errors.NewValidationError("Password cannot be empty") }
if err := s.repo.UpdatePassword(id, password); err != nil { return err }
s.log.Printf("Changed password for %s", id)
return nil
}
func (s *AccountService) ChangeEmail(id string, email string) error {
if len(email) == 0 { return s.repo.RemoveEmail(id) }
if err := s.repo.UpdateEmail(id, email); err != nil { return err }
s.log.Printf("Changed email for %s to '%s'", id, email)
return nil
}
func (s *AccountService) ChangeAvatarURL(id string, avatarURL string) error {
if len(avatarURL) == 0 { return s.repo.RemoveAvatar(id) }
if err := s.repo.UpdateAvatarURL(id, avatarURL); err != nil { return err }
s.log.Printf("Changed avatar URL for %s to '%s'", id, avatarURL)
return nil
}
func (s *AccountService) IncrementFails(id string) (int, error) {
num, err := s.repo.IncrementFails(id)
if err != nil { return 0, err }
s.log.Printf("Incremented auth failures for account %s (now %d)", id, num)
return num, nil
}
func (s *AccountService) ResetFails(id string) (error) {
if err := s.repo.ResetFails(id); err != nil { return err }
s.log.Printf("Reset auth failures for account %s", id)
return nil
}
func (s *AccountService) Lock(id string) error {
if err := s.repo.SetLocked(id, true); err != nil { return err }
s.log.Printf("Locked account %s", id)
return nil
}
func (s *AccountService) Unlock(id string) error {
if err := s.repo.SetLocked(id, false); err != nil { return err }
s.log.Printf("Unlocked account %s", id)
return nil
}
func (s *AccountService) Delete(id string) error {
deletedID, err := s.repo.Delete(id)
if err != nil { return err }
if deletedID == "" { return errors.NewNotExistError("Account does not exist") }
s.log.Printf("Deleted account %s", id)
return nil
}