51 lines
1.4 KiB
Go
51 lines
1.4 KiB
Go
package memory
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"git.threesix.ai/jordan/slate-v3-1770514618/services/preferences-api/internal/domain"
|
|
"git.threesix.ai/jordan/slate-v3-1770514618/services/preferences-api/internal/port"
|
|
)
|
|
|
|
var _ port.PreferencesRepository = (*PreferencesRepository)(nil)
|
|
|
|
// PreferencesRepository is a thread-safe in-memory implementation of port.PreferencesRepository.
|
|
type PreferencesRepository struct {
|
|
mu sync.RWMutex
|
|
prefs map[domain.UserID]*domain.Preferences
|
|
}
|
|
|
|
// NewPreferencesRepository creates a new in-memory preferences repository.
|
|
func NewPreferencesRepository() *PreferencesRepository {
|
|
return &PreferencesRepository{
|
|
prefs: make(map[domain.UserID]*domain.Preferences),
|
|
}
|
|
}
|
|
|
|
// Get returns preferences for a user.
|
|
// Returns domain.ErrPreferencesNotFound if not found.
|
|
func (r *PreferencesRepository) Get(_ context.Context, userID domain.UserID) (*domain.Preferences, error) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
p, ok := r.prefs[userID]
|
|
if !ok {
|
|
return nil, domain.ErrPreferencesNotFound
|
|
}
|
|
cp := *p
|
|
cp.Notifications = p.Notifications
|
|
return &cp, nil
|
|
}
|
|
|
|
// Upsert stores preferences for a user (insert or replace).
|
|
func (r *PreferencesRepository) Upsert(_ context.Context, userID domain.UserID, prefs *domain.Preferences) error {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
|
|
cp := *prefs
|
|
cp.Notifications = prefs.Notifications
|
|
r.prefs[userID] = &cp
|
|
return nil
|
|
}
|