further tidying up account service/repo
This commit is contained in:
parent
32bcb894ee
commit
9e311df462
4 changed files with 26 additions and 38 deletions
|
|
@ -18,13 +18,13 @@ type AccountRepository interface {
|
|||
// Create an account, returning the new account ID.
|
||||
Create(username string, password string, email *string, avatarURL *string) (string, error)
|
||||
|
||||
// Deprecated in favour of more specialised Change* and Remove* functions.
|
||||
// Deprecated in favour of more specialised Update* and Remove* functions.
|
||||
Update(id string, username string, password string, email *string, avatarUrl *string) error
|
||||
ChangeUsername(id string, username string) error
|
||||
ChangePassword(id string, password string) error
|
||||
ChangeEmail(id string, email string) error
|
||||
UpdateUsername(id string, username string) error
|
||||
UpdatePassword(id string, password string) error
|
||||
UpdateEmail(id string, email string) error
|
||||
RemoveEmail(id string) error
|
||||
ChangeAvatarURL(id string, avatarURL string) error
|
||||
UpdateAvatarURL(id string, avatarURL string) error
|
||||
RemoveAvatar(id string) error
|
||||
|
||||
Delete(id string) error
|
||||
|
|
@ -33,6 +33,5 @@ type AccountRepository interface {
|
|||
// returning the current fail count.
|
||||
IncrementFails(id string) (int, error)
|
||||
ResetFails(id string) error
|
||||
Lock(id string) error
|
||||
Unlock(id string) error
|
||||
SetLocked(id string, lock bool) error
|
||||
}
|
||||
|
|
|
|||
|
|
@ -71,7 +71,7 @@ func (repo *AccountRepositoryMemory) Create(username string, password string, em
|
|||
}
|
||||
|
||||
// Intended for large profile updates. For smaller adjusments,
|
||||
// more specialised Change* and Remove* functions should be used.
|
||||
// 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")
|
||||
|
|
@ -91,7 +91,7 @@ func (repo *AccountRepositoryMemory) Update(id string, username string, password
|
|||
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangeUsername(id string, username string) error {
|
||||
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")
|
||||
} else if account != nil && account.ID != id {
|
||||
|
|
@ -105,7 +105,7 @@ func (repo *AccountRepositoryMemory) ChangeUsername(id string, username string)
|
|||
account.Username = username
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangePassword(id string, password string) error {
|
||||
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") }
|
||||
|
|
@ -113,7 +113,7 @@ func (repo *AccountRepositoryMemory) ChangePassword(id string, password string)
|
|||
account.Password = password
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangeEmail(id string, email string) error {
|
||||
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") }
|
||||
|
|
@ -131,7 +131,7 @@ func (repo *AccountRepositoryMemory) RemoveEmail(id string) error {
|
|||
account.Email.String = ""
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) ChangeAvatarURL(id string, avatarURL 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") }
|
||||
|
|
@ -182,15 +182,9 @@ func (repo *AccountRepositoryMemory) ResetFails(id string) error {
|
|||
account.FailAttempts = 0
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) Lock(id string) error {
|
||||
func (repo *AccountRepositoryMemory) SetLocked(id string, locked bool) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
account.Locked = true
|
||||
return nil
|
||||
}
|
||||
func (repo *AccountRepositoryMemory) Unlock(id string) error {
|
||||
account, err := repo.GetByID(id)
|
||||
if err != nil { return err }
|
||||
account.Locked = false
|
||||
account.Locked = locked
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -120,21 +120,21 @@ func (repo *AccountRepositoryPostgres) Update(
|
|||
return err
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryPostgres) ChangeUsername(id string, username string) error {
|
||||
func (repo *AccountRepositoryPostgres) UpdateUsername(id string, username string) error {
|
||||
_, err := repo.db.Exec(
|
||||
"UPDATE account SET username=$2 WHERE id=$1",
|
||||
id, username,
|
||||
)
|
||||
return err
|
||||
}
|
||||
func (repo *AccountRepositoryPostgres) ChangePassword(id string, password string) error {
|
||||
func (repo *AccountRepositoryPostgres) UpdatePassword(id string, password string) error {
|
||||
_, err := repo.db.Exec(
|
||||
"UPDATE account SET password=$2 WHERE id=$1",
|
||||
id, password,
|
||||
)
|
||||
return err
|
||||
}
|
||||
func (repo *AccountRepositoryPostgres) ChangeEmail(id string, email string) error {
|
||||
func (repo *AccountRepositoryPostgres) UpdateEmail(id string, email string) error {
|
||||
_, err := repo.db.Exec(
|
||||
"UPDATE account SET email=$2 WHERE id=$1",
|
||||
id, email,
|
||||
|
|
@ -145,7 +145,7 @@ func (repo *AccountRepositoryPostgres) RemoveEmail(id string) error {
|
|||
_, err := repo.db.Exec("UPDATE account SET email=NULL WHERE id=$1", id)
|
||||
return err
|
||||
}
|
||||
func (repo *AccountRepositoryPostgres) ChangeAvatarURL(id string, avatarURL string) error {
|
||||
func (repo *AccountRepositoryPostgres) UpdateAvatarURL(id string, avatarURL string) error {
|
||||
_, err := repo.db.Exec(
|
||||
"UPDATE account SET avatar_url=$2 WHERE id=$1",
|
||||
id, avatarURL,
|
||||
|
|
@ -175,12 +175,7 @@ func (repo *AccountRepositoryPostgres) ResetFails(id string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryPostgres) Lock(id string) error {
|
||||
_, err := repo.db.Exec("UPDATE account SET locked = true WHERE id=$1", id)
|
||||
return err
|
||||
}
|
||||
|
||||
func (repo *AccountRepositoryPostgres) Unlock(id string) error {
|
||||
_, err := repo.db.Exec("UPDATE account SET locked = false, fail_attempts = 0 WHERE id=$1", id)
|
||||
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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,7 +81,7 @@ 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.ChangeUsername(id, username); err != nil {
|
||||
if err := s.repo.UpdateAvatarURL(id, username); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ func (s *AccountService) ChangeUsername(id string, username string) error {
|
|||
}
|
||||
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 {
|
||||
if err := s.repo.UpdateAvatarURL(id, password); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -99,7 +99,7 @@ func (s *AccountService) ChangePassword(id string, password string) error {
|
|||
}
|
||||
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 {
|
||||
if err := s.repo.UpdateAvatarURL(id, email); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -108,7 +108,7 @@ func (s *AccountService) ChangeEmail(id string, email string) error {
|
|||
}
|
||||
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 {
|
||||
if err := s.repo.UpdateAvatarURL(id, avatarURL); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
@ -136,13 +136,13 @@ func (s *AccountService) ResetFails(id string) (error) {
|
|||
}
|
||||
|
||||
func (s *AccountService) Lock(id string) error {
|
||||
if err := s.repo.Lock(id); err != nil { return err }
|
||||
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.Unlock(id); err != nil { return err }
|
||||
s.log.Printf("Locked account %s", id)
|
||||
if err := s.repo.SetLocked(id, false); err != nil { return err }
|
||||
s.log.Printf("Unlocked account %s", id)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue