functional simple api
This commit is contained in:
@@ -4,20 +4,20 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"go-nkode/core/model"
|
||||
m "go-nkode/core/model"
|
||||
"go-nkode/hashset"
|
||||
py "go-nkode/py-builtin"
|
||||
"go-nkode/util"
|
||||
)
|
||||
|
||||
type Customer struct {
|
||||
CustomerId uuid.UUID
|
||||
NKodePolicy model.NKodePolicy
|
||||
CustomerId m.CustomerId
|
||||
NKodePolicy m.NKodePolicy
|
||||
Attributes CustomerAttributes
|
||||
Users map[string]User
|
||||
Users map[m.Username]User
|
||||
}
|
||||
|
||||
func NewCustomer(keypadSize model.KeypadSize, nkodePolicy model.NKodePolicy) (*Customer, error) {
|
||||
func NewCustomer(keypadSize m.KeypadSize, nkodePolicy m.NKodePolicy) (*Customer, error) {
|
||||
if keypadSize.TotalAttrs() < nkodePolicy.DistinctAttributes {
|
||||
return nil, errors.New(fmt.Sprintf("incompadible nkode policy and keypad size TotalAttrs: %d < DistinctAttributes: %d", keypadSize.TotalAttrs(), nkodePolicy.DistinctAttributes))
|
||||
}
|
||||
@@ -30,19 +30,19 @@ func NewCustomer(keypadSize model.KeypadSize, nkodePolicy model.NKodePolicy) (*C
|
||||
return nil, err
|
||||
}
|
||||
customer := Customer{
|
||||
CustomerId: uuid.New(),
|
||||
CustomerId: m.CustomerId(uuid.New()),
|
||||
NKodePolicy: nkodePolicy,
|
||||
Attributes: *customerAttrs,
|
||||
Users: make(map[string]User),
|
||||
Users: make(map[m.Username]User),
|
||||
}
|
||||
|
||||
return &customer, nil
|
||||
}
|
||||
|
||||
func (c *Customer) AddNewUser(username string, passcodeIdx []int, userInterface UserInterface) error {
|
||||
func (c *Customer) AddNewUser(username m.Username, passcodeIdx []int, userInterface UserInterface) error {
|
||||
_, exists := c.Users[username]
|
||||
if exists {
|
||||
return errors.New(fmt.Sprintf("User %s already exists for customer %s exists", username, c.CustomerId.String()))
|
||||
return errors.New(fmt.Sprintf("User %s already exists for customer %+v exists", username, c.CustomerId))
|
||||
}
|
||||
newKeys, err := NewUserCipherKeys(c.Attributes.KeypadSize, c.Attributes.SetVals, c.NKodePolicy.MaxNkodeLen)
|
||||
if err != nil {
|
||||
@@ -62,10 +62,10 @@ func (c *Customer) AddNewUser(username string, passcodeIdx []int, userInterface
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) ValidKeyEntry(username string, selectedKeys []int) ([]int, error) {
|
||||
func (c *Customer) ValidKeyEntry(username m.Username, selectedKeys []int) ([]int, error) {
|
||||
user, exists := c.Users[username]
|
||||
if !exists {
|
||||
return nil, errors.New(fmt.Sprintf("user %s does not exist for customer %s", username, c.CustomerId.String()))
|
||||
return nil, errors.New(fmt.Sprintf("user %s does not exist for customer %+v", username, c.CustomerId))
|
||||
}
|
||||
|
||||
validKeys := py.All[int](selectedKeys, func(idx int) bool {
|
||||
@@ -152,10 +152,10 @@ func (c *Customer) IsValidNKode(passcodeAttrIdx []int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Customer) GetLoginInterface(username string) ([]int, error) {
|
||||
func (c *Customer) GetLoginInterface(username m.Username) ([]int, error) {
|
||||
user, exists := c.Users[username]
|
||||
if !exists {
|
||||
return nil, errors.New(fmt.Sprintf("can't get login interface for non-existant user %s in customer %s", username, c.CustomerId.String()))
|
||||
return nil, errors.New(fmt.Sprintf("can't get login interface for non-existant user %s in customer %s", username, c.CustomerId))
|
||||
}
|
||||
err := user.Interface.PartialInterfaceShuffle()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user