2021-03-14 13:08:50 +13:00
|
|
|
package member
|
|
|
|
|
|
|
|
import (
|
2021-03-15 04:58:18 +13:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
2021-03-14 13:08:50 +13:00
|
|
|
"demodesk/neko/internal/config"
|
|
|
|
"demodesk/neko/internal/member/dummy"
|
|
|
|
"demodesk/neko/internal/member/file"
|
|
|
|
"demodesk/neko/internal/member/object"
|
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
)
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func New(config *config.Member) *MemberManagerCtx {
|
|
|
|
manager := &MemberManagerCtx{
|
|
|
|
logger: log.With().Str("module", "member").Logger(),
|
|
|
|
config: config,
|
|
|
|
}
|
|
|
|
|
2021-03-14 13:08:50 +13:00
|
|
|
switch config.Provider {
|
|
|
|
case "file":
|
2021-03-15 04:58:18 +13:00
|
|
|
manager.provider = file.New(file.Config{
|
2021-03-15 01:07:03 +13:00
|
|
|
File: config.FilePath,
|
|
|
|
})
|
2021-03-14 13:08:50 +13:00
|
|
|
case "object":
|
2021-03-15 04:58:18 +13:00
|
|
|
manager.provider = object.New(object.Config{
|
2021-03-15 01:07:03 +13:00
|
|
|
AdminPassword: config.AdminPassword,
|
|
|
|
UserPassword: config.Password,
|
|
|
|
})
|
2021-03-14 13:08:50 +13:00
|
|
|
case "dummy":
|
2021-03-15 04:58:18 +13:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
manager.provider = dummy.New()
|
2021-03-14 13:08:50 +13:00
|
|
|
}
|
2021-03-15 04:58:18 +13:00
|
|
|
|
|
|
|
return manager
|
|
|
|
}
|
|
|
|
|
|
|
|
type MemberManagerCtx struct {
|
|
|
|
logger zerolog.Logger
|
|
|
|
config *config.Member
|
|
|
|
mu sync.Mutex
|
|
|
|
provider types.MemberProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) Connect() error {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Connect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) Disconnect() error {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Disconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) Authenticate(username string, password string) (string, types.MemberProfile, error) {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Authenticate(username, password)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) Insert(username string, password string, profile types.MemberProfile) (string, error) {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Insert(username, password, profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) Select(id string) (types.MemberProfile, error) {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Select(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) SelectAll(limit int, offset int) (map[string]types.MemberProfile, error) {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.SelectAll(limit, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) UpdateProfile(id string, profile types.MemberProfile) error {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.UpdateProfile(id, profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *MemberManagerCtx) UpdatePassword(id string, password string) error {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.UpdatePassword(id, password)
|
|
|
|
}
|
2021-03-14 13:08:50 +13:00
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
func (manager *MemberManagerCtx) Delete(id string) error {
|
2021-03-15 07:36:30 +13:00
|
|
|
manager.mu.Lock()
|
|
|
|
defer manager.mu.Unlock()
|
|
|
|
|
2021-03-15 04:58:18 +13:00
|
|
|
return manager.provider.Delete(id)
|
2021-03-14 13:08:50 +13:00
|
|
|
}
|