make service handling of non-error-not-founds explicit
This commit is contained in:
parent
6eb204bfce
commit
32bcb894ee
2 changed files with 24 additions and 3 deletions
|
|
@ -5,6 +5,8 @@ import "arimelody-web/model"
|
||||||
type AccountRepository interface {
|
type AccountRepository interface {
|
||||||
GetAll() ([]*model.Account, error)
|
GetAll() ([]*model.Account, error)
|
||||||
GetCount() (int, error)
|
GetCount() (int, error)
|
||||||
|
// Fetches an account by ID, returning an error if one was encountered.
|
||||||
|
// If the account does not exist, both response fields are nil.
|
||||||
GetByID(id string) (*model.Account, error)
|
GetByID(id string) (*model.Account, error)
|
||||||
GetByUsername(username string) (*model.Account, error)
|
GetByUsername(username string) (*model.Account, error)
|
||||||
GetByEmail(email string) (*model.Account, error)
|
GetByEmail(email string) (*model.Account, error)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"arimelody-web/model"
|
"arimelody-web/model"
|
||||||
repository "arimelody-web/repository/account"
|
repository "arimelody-web/repository/account"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,15 +29,33 @@ func (s *AccountService) GetCount() (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *AccountService) GetByID(id string) (*model.Account, error) {
|
func (s *AccountService) GetByID(id string) (*model.Account, error) {
|
||||||
return s.repo.GetByID(id)
|
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) {
|
func (s *AccountService) GetByUsername(username string) (*model.Account, error) {
|
||||||
return s.repo.GetByUsername(username)
|
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) {
|
func (s *AccountService) GetByEmail(email string) (*model.Account, error) {
|
||||||
return s.repo.GetByEmail(email)
|
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(
|
func (s *AccountService) Create(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue