57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package sub
|
|
|
|
import (
|
|
"sync"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/omnex/control-plane/api/internal/auth"
|
|
)
|
|
|
|
// MemCodeStore : implémentation en mémoire de CodeStore (dev/tests).
|
|
type MemCodeStore struct {
|
|
mu sync.RWMutex
|
|
items map[string]CodeBuySub
|
|
byCode map[string]CodeBuySub
|
|
}
|
|
|
|
func NewMemCodeStore() *MemCodeStore {
|
|
return &MemCodeStore{
|
|
items: make(map[string]CodeBuySub),
|
|
byCode: make(map[string]CodeBuySub),
|
|
}
|
|
}
|
|
|
|
func (m *MemCodeStore) CreateCodeForSub(username, code string) (CodeBuySub, error) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
c := CodeBuySub{
|
|
ID: uuid.NewString(),
|
|
Username: username,
|
|
CodeBuy: code,
|
|
}
|
|
m.items[c.ID] = c
|
|
m.byCode[code] = c
|
|
return c, nil
|
|
}
|
|
|
|
func (m *MemCodeStore) GetCodeForSub(username, code string) (auth.User, CodeBuySub, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
c, ok := m.byCode[code]
|
|
if !ok {
|
|
return auth.User{}, CodeBuySub{}, nil
|
|
}
|
|
// In a real implementation, we'd fetch the user too
|
|
return auth.User{}, c, nil
|
|
}
|
|
|
|
func (m *MemCodeStore) ListCodes() ([]CodeBuySub, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
out := make([]CodeBuySub, 0, len(m.items))
|
|
for _, c := range m.items {
|
|
out = append(out, c)
|
|
}
|
|
return out, nil
|
|
}
|