package account import ( "arimelody-web/model" repository "arimelody-web/repository/account" "errors" "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) { return s.repo.GetByID(id) } func (s *AccountService) GetByUsername(username string) (*model.Account, error) { return s.repo.GetByUsername(username) } func (s *AccountService) GetByEmail(email string) (*model.Account, error) { return s.repo.GetByEmail(email) } 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 } // Intended for large profile updates. For smaller adjusments, // more specialised Change* and Remove* functions should be used. func (s *AccountService) Update( id string, username string, password string, email *string, avatarUrl *string, ) error { if len(username) == 0 { return errors.New("Username cannot be empty") } if len(password) == 0 { return errors.New("Password cannot be empty") } if email != nil && len(*email) == 0 { return errors.New("Email cannot be empty") } if err := s.repo.Update(id, username, password, email, avatarUrl); err != nil { return err } s.log.Printf("Updated account '%s' (%s)", username, id) return 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.ChangeUsername(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.ChangePassword(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.ChangeEmail(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.ChangeAvatarURL(id, avatarURL); err != nil { return err } s.log.Printf("Changed avatar URL for %s to '%s'", id, avatarURL) return nil } func (s *AccountService) Delete(accountID string) error { return s.repo.Delete(accountID) } func (s *AccountService) IncrementFails(accountID string) (int, error) { return s.repo.IncrementFails(accountID) } func (s *AccountService) ResetFails(accountID string) (error) { return s.repo.ResetFails(accountID) } func (s *AccountService) Lock(accountID string) error { return s.repo.Lock(accountID) } func (s *AccountService) Unlock(accountID string) error { return s.repo.Unlock(accountID) }