arimelody-web/service/account/account.go

148 lines
4.2 KiB
Go

package account
import (
"arimelody-web/model"
repository "arimelody-web/repository/account"
"errors"
"fmt"
"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) {
if account, err := s.repo.GetByID(id); err != nil {
return nil, err
} else if account == nil {
return nil, fmt.Errorf("Account does not exist: %s", id)
} else {
return account, nil
}
}
func (s *AccountService) GetByUsername(username string) (*model.Account, error) {
if account, err := s.repo.GetByUsername(username); err != nil {
return nil, err
} else if account == nil {
return nil, fmt.Errorf("Account does not exist: %s", username)
} else {
return account, nil
}
}
func (s *AccountService) GetByEmail(email string) (*model.Account, error) {
if account, err := s.repo.GetByEmail(email); err != nil {
return nil, err
} else if account == nil {
return nil, fmt.Errorf("Account does not exist with email: %s", email)
} else {
return account, nil
}
}
func (s *AccountService) Create(
username string,
password string,
email *string,
avatarURL *string,
) (string, error) {
var id string
var err error
if len(username) == 0 { return id, errors.New("Username cannot be empty") }
if len(password) == 0 { return id, errors.New("Password cannot be empty") }
if email != nil && len(*email) == 0 { return id, errors.New("Email cannot be empty") }
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.New("Username cannot be empty") }
if err := s.repo.UpdateAvatarURL(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.New("Password cannot be empty") }
if err := s.repo.UpdateAvatarURL(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.UpdateAvatarURL(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) Delete(id string) error {
if err := s.repo.Delete(id); err != nil { return err }
s.log.Printf("Deleted account %s", id)
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
}