members minor bugfixes.

This commit is contained in:
Miroslav Šedivý 2021-03-14 14:44:03 +01:00
parent 8f277c61c9
commit 4898cecd56
4 changed files with 25 additions and 27 deletions

View File

@ -44,7 +44,7 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
return map[string]types.MemberProfile{}, fmt.Errorf("Not implemented.") return map[string]types.MemberProfile{}, fmt.Errorf("Not implemented.")
} }
func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
return fmt.Errorf("Not implemented.") return fmt.Errorf("Not implemented.")
} }

View File

@ -62,20 +62,21 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
return "", err return "", err
} }
entry, ok := entries[id] _, ok := entries[id]
if ok { if ok {
return "", fmt.Errorf("Member ID already exists.") return "", fmt.Errorf("Member ID already exists.")
} }
// TODO: Use hash function. entries[id] = MemberEntry{
entry.Password = password // TODO: Use hash function.
entry.Profile = profile Password: password,
entries[id] = entry Profile: profile,
}
return id, manager.serialize(entries) return id, manager.serialize(entries)
} }
func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -141,11 +142,10 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
i := 0 i := 0
for id, entry := range entries { for id, entry := range entries {
if i < offset || i > offset+limit { if i >= offset && (limit == 0 || i < offset+limit) {
continue profiles[id] = entry.Profile
} }
profiles[id] = entry.Profile
i = i + 1 i = i + 1
} }

View File

@ -10,14 +10,14 @@ import (
func New(config Config) types.MemberManager { func New(config Config) types.MemberManager {
return &MemberManagerCtx{ return &MemberManagerCtx{
config: config, config: config,
entries: make(map[string]MemberEntry), entries: make(map[string]*MemberEntry),
mu: sync.Mutex{}, mu: sync.Mutex{},
} }
} }
type MemberManagerCtx struct { type MemberManagerCtx struct {
config Config config Config
entries map[string]MemberEntry entries map[string]*MemberEntry
mu sync.Mutex mu sync.Mutex
} }
@ -84,20 +84,21 @@ func (manager *MemberManagerCtx) Insert(username string, password string, profil
// id will be also username // id will be also username
id := username id := username
entry, ok := manager.entries[id] _, ok := manager.entries[id]
if ok { if ok {
return "", fmt.Errorf("Member ID already exists.") return "", fmt.Errorf("Member ID already exists.")
} }
// TODO: Use hash function. manager.entries[id] = &MemberEntry{
entry.Password = password // TODO: Use hash function.
entry.Profile = profile Password: password,
manager.entries[id] = entry Profile: profile,
}
return id, nil return id, nil
} }
func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile) error { func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
@ -107,7 +108,6 @@ func (manager *MemberManagerCtx) Update(id string, profile types.MemberProfile)
} }
entry.Profile = profile entry.Profile = profile
manager.entries[id] = entry
return nil return nil
} }
@ -123,7 +123,6 @@ func (manager *MemberManagerCtx) UpdatePassword(id string, password string) erro
// TODO: Use hash function. // TODO: Use hash function.
entry.Password = password entry.Password = password
manager.entries[id] = entry
return nil return nil
} }
@ -133,8 +132,8 @@ func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error)
defer manager.mu.Unlock() defer manager.mu.Unlock()
entry, ok := manager.entries[id] entry, ok := manager.entries[id]
if ok { if !ok {
return types.MemberProfile{}, fmt.Errorf("Member ID already exists.") return types.MemberProfile{}, fmt.Errorf("Member ID does not exist.")
} }
return entry.Profile, nil return entry.Profile, nil
@ -144,15 +143,14 @@ func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]ty
manager.mu.Lock() manager.mu.Lock()
defer manager.mu.Unlock() defer manager.mu.Unlock()
profiles := map[string]types.MemberProfile{} profiles := make(map[string]types.MemberProfile)
i := 0 i := 0
for id, entry := range manager.entries { for id, entry := range manager.entries {
if i < offset || i > offset+limit { if i >= offset && (limit == 0 || i < offset+limit) {
continue profiles[id] = entry.Profile
} }
profiles[id] = entry.Profile
i = i + 1 i = i + 1
} }

View File

@ -19,7 +19,7 @@ type MemberManager interface {
Insert(username string, password string, profile MemberProfile) (string, error) Insert(username string, password string, profile MemberProfile) (string, error)
Select(id string) (MemberProfile, error) Select(id string) (MemberProfile, error)
SelectAll(limit int, offset int) (map[string]MemberProfile, error) SelectAll(limit int, offset int) (map[string]MemberProfile, error)
Update(id string, profile MemberProfile) error UpdateProfile(id string, profile MemberProfile) error
UpdatePassword(id string, passwrod string) error UpdatePassword(id string, passwrod string) error
Delete(id string) error Delete(id string) error
} }