Refactor into server object to handle registered accounts
This commit is contained in:
parent
eccb726f74
commit
93decc027b
13 changed files with 304 additions and 128 deletions
47
pkg/server/accounts.go
Normal file
47
pkg/server/accounts.go
Normal file
|
@ -0,0 +1,47 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Account represents a registered user
|
||||
type Account struct {
|
||||
// Name simply describes the account and must be unique
|
||||
Name string
|
||||
|
||||
// id represents a unique ID per account and is set one registered
|
||||
id uuid.UUID
|
||||
}
|
||||
|
||||
// Accountant manages a set of accounts
|
||||
type Accountant struct {
|
||||
accounts []Account
|
||||
}
|
||||
|
||||
// NewAccountant creates a new accountant
|
||||
func NewAccountant() *Accountant {
|
||||
return &Accountant{}
|
||||
}
|
||||
|
||||
// RegisterAccount adds an account to the set of internal accounts
|
||||
func (a *Accountant) RegisterAccount(acc Account) (Account, error) {
|
||||
|
||||
// Set the account ID to a new UUID
|
||||
acc.id = uuid.New()
|
||||
|
||||
// Verify this acount isn't already registered
|
||||
for _, a := range a.accounts {
|
||||
if a.Name == acc.Name {
|
||||
return Account{}, fmt.Errorf("Account name already registered")
|
||||
} else if a.id == acc.id {
|
||||
return Account{}, fmt.Errorf("Account ID already registered")
|
||||
}
|
||||
}
|
||||
|
||||
// Simply add the account to the list
|
||||
a.accounts = append(a.accounts, acc)
|
||||
|
||||
return acc, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue