HEAVY: finish music service migration, tidy up services, more tests
This commit is contained in:
parent
9e311df462
commit
90a671982c
47 changed files with 2698 additions and 902 deletions
|
|
@ -27,11 +27,11 @@ type AccountRepository interface {
|
|||
UpdateAvatarURL(id string, avatarURL string) error
|
||||
RemoveAvatar(id string) error
|
||||
|
||||
Delete(id string) error
|
||||
|
||||
// Increment the number of account login failure attempts,
|
||||
// returning the current fail count.
|
||||
IncrementFails(id string) (int, error)
|
||||
ResetFails(id string) error
|
||||
SetLocked(id string, lock bool) error
|
||||
|
||||
Delete(id string) (string, error)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,9 +1,10 @@
|
|||
package account
|
||||
|
||||
import (
|
||||
"arimelody-web/errors"
|
||||
"arimelody-web/model"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
|
|
@ -24,10 +25,11 @@ func (repo *AccountRepositoryMemory) GetCount() (int, error) {
|
|||
return len(repo.accounts), nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) GetByID(id string) (*model.Account, error) {
|
||||
for _, account := range repo.accounts {
|
||||
if account.ID == id { return account, nil }
|
||||
}
|
||||
return nil, nil
|
||||
index := slices.IndexFunc(repo.accounts, func(account *model.Account) bool {
|
||||
return account.ID == id
|
||||
})
|
||||
if index == -1 { return nil, nil }
|
||||
return repo.accounts[index], nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) GetByUsername(username string) (*model.Account, error) {
|
||||
for _, account := range repo.accounts {
|
||||
|
|
@ -47,9 +49,9 @@ func (repo *AccountRepositoryMemory) GetByEmail(email string) (*model.Account, e
|
|||
// Create an account, returning the new account ID.
|
||||
func (repo *AccountRepositoryMemory) Create(username string, password string, email *string, avatarURL *string) (string, error) {
|
||||
if account, err := repo.GetByUsername(username); err != nil {
|
||||
return "", errors.New("Failed to fetch other acccounts by username")
|
||||
return "", errors.NewNotExistError("Failed to fetch other acccounts by username")
|
||||
} else if account != nil {
|
||||
return "", errors.New("Account with this username already exists")
|
||||
return "", errors.NewNotExistError("Account with this username already exists")
|
||||
}
|
||||
|
||||
emailRef := ""
|
||||
|
|
@ -74,9 +76,9 @@ func (repo *AccountRepositoryMemory) Create(username string, password string, em
|
|||
// more specialised Update* and Remove* functions should be used.
|
||||
func (repo *AccountRepositoryMemory) Update(id string, username string, password string, email *string, avatarUrl *string) error {
|
||||
if account, err := repo.GetByUsername(username); err != nil {
|
||||
return errors.New("Failed to fetch other acccounts by username")
|
||||
return errors.NewNotExistError("Failed to fetch other acccounts by username")
|
||||
} else if account != nil && account.ID != id {
|
||||
return errors.New("Account with this username already exists")
|
||||
return errors.NewNotExistError("Account with this username already exists")
|
||||
}
|
||||
|
||||
account, err := repo.GetByID(id)
|
||||
|
|
@ -93,14 +95,14 @@ func (repo *AccountRepositoryMemory) Update(id string, username string, password
|
|||
}
|
||||
func (repo *AccountRepositoryMemory) UpdateUsername(id string, username string) error {
|
||||
if account, err := repo.GetByUsername(username); err != nil {
|
||||
return errors.New("Failed to fetch other acccounts by username")
|
||||
return errors.NewNotExistError("Failed to fetch other acccounts by username")
|
||||
} else if account != nil && account.ID != id {
|
||||
return errors.New("Account with this username already exists")
|
||||
return errors.NewNotExistError("Account with this username already exists")
|
||||
}
|
||||
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.Username = username
|
||||
return nil
|
||||
|
|
@ -108,7 +110,7 @@ func (repo *AccountRepositoryMemory) UpdateUsername(id string, username string)
|
|||
func (repo *AccountRepositoryMemory) UpdatePassword(id string, password string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.Password = password
|
||||
return nil
|
||||
|
|
@ -116,7 +118,7 @@ func (repo *AccountRepositoryMemory) UpdatePassword(id string, password string)
|
|||
func (repo *AccountRepositoryMemory) UpdateEmail(id string, email string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.Email.Valid = true
|
||||
account.Email.String = email
|
||||
|
|
@ -125,7 +127,7 @@ func (repo *AccountRepositoryMemory) UpdateEmail(id string, email string) error
|
|||
func (repo *AccountRepositoryMemory) RemoveEmail(id string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.Email.Valid = false
|
||||
account.Email.String = ""
|
||||
|
|
@ -134,7 +136,7 @@ func (repo *AccountRepositoryMemory) RemoveEmail(id string) error {
|
|||
func (repo *AccountRepositoryMemory) UpdateAvatarURL(id string, avatarURL string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.AvatarURL.Valid = true
|
||||
account.AvatarURL.String = avatarURL
|
||||
|
|
@ -143,31 +145,13 @@ func (repo *AccountRepositoryMemory) UpdateAvatarURL(id string, avatarURL string
|
|||
func (repo *AccountRepositoryMemory) RemoveAvatar(id string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
if account == nil { return errors.New("Account does not exist") }
|
||||
if account == nil { return errors.NewNotExistError("Account does not exist") }
|
||||
|
||||
account.AvatarURL.Valid = false
|
||||
account.AvatarURL.String = ""
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryMemory) Delete(id string) error {
|
||||
accountIndex := -1
|
||||
for index, account := range repo.accounts {
|
||||
if account.ID == id {
|
||||
accountIndex = index
|
||||
break
|
||||
}
|
||||
}
|
||||
if accountIndex == -1 { return nil }
|
||||
|
||||
repo.accounts = append(
|
||||
repo.accounts[:accountIndex],
|
||||
repo.accounts[accountIndex+1:]...,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Increment the number of account login failure attempts,
|
||||
// returning the current fail count.
|
||||
func (repo *AccountRepositoryMemory) IncrementFails(id string) (int, error) {
|
||||
|
|
@ -188,3 +172,17 @@ func (repo *AccountRepositoryMemory) SetLocked(id string, locked bool) error {
|
|||
account.Locked = locked
|
||||
return nil
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryMemory) Delete(id string) (string, error) {
|
||||
var deletedID string
|
||||
newAccounts := []*model.Account{}
|
||||
for _, account := range repo.accounts {
|
||||
if account.ID == id {
|
||||
deletedID = id
|
||||
continue
|
||||
}
|
||||
newAccounts = append(newAccounts, account)
|
||||
}
|
||||
repo.accounts = newAccounts
|
||||
return deletedID, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -157,11 +157,6 @@ func (repo *AccountRepositoryPostgres) RemoveAvatar(id string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryPostgres) Delete(id string) error {
|
||||
_, err := repo.db.Exec("DELETE FROM account WHERE id=$1", id)
|
||||
return err
|
||||
}
|
||||
|
||||
// Increment the number of account login failure attempts,
|
||||
// returning the current fail count.
|
||||
func (repo *AccountRepositoryPostgres) IncrementFails(id string) (int, error) {
|
||||
|
|
@ -179,3 +174,9 @@ func (repo *AccountRepositoryPostgres) SetLocked(id string, locked bool) error {
|
|||
_, err := repo.db.Exec("UPDATE account SET locked = $2 WHERE id=$1", id, locked)
|
||||
return err
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryPostgres) Delete(id string) (string, error) {
|
||||
var deletedID string
|
||||
err := repo.db.Get(&deletedID, "DELETE FROM account WHERE id=$1", id)
|
||||
return deletedID, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue