HEAVY: finish music service migration, tidy up services, more tests

This commit is contained in:
ari melody 2026-08-01 00:29:31 +01:00
parent 9e311df462
commit 90a671982c
Signed by: ari
GPG key ID: CF99829C92678188
47 changed files with 2698 additions and 902 deletions

View file

@ -1,10 +1,10 @@
package account
import (
"arimelody-web/errors"
"arimelody-web/model"
repository "arimelody-web/repository/account"
"errors"
"fmt"
"arimelody-web/service/validator"
"log"
)
@ -29,33 +29,24 @@ func (s *AccountService) GetCount() (int, error) {
}
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
}
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) {
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
}
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) {
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
}
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(
@ -64,13 +55,13 @@ func (s *AccountService) Create(
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 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
}
@ -80,48 +71,31 @@ func (s *AccountService) Create(
}
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
}
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.New("Password cannot be empty") }
if err := s.repo.UpdateAvatarURL(id, password); err != nil {
return err
}
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.UpdateAvatarURL(id, email); err != nil {
return err
}
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
}
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 }
@ -146,3 +120,11 @@ func (s *AccountService) Unlock(id string) error {
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
}