slack5-1770574304/services/preferences-api/internal/adapter/memory/preferences.go
rdev-worker 5fa5a77bfb
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
build: /implement-feature user-preferences
2026-02-08 18:36:52 +00:00

51 lines
1.3 KiB
Go

package memory
import (
"context"
"sync"
"git.threesix.ai/jordan/slack5-1770574304/services/preferences-api/internal/domain"
"git.threesix.ai/jordan/slack5-1770574304/services/preferences-api/internal/port"
)
// Compile-time verification that PreferenceRepository implements port.PreferenceRepository.
var _ port.PreferenceRepository = (*PreferenceRepository)(nil)
// PreferenceRepository is a thread-safe in-memory implementation of port.PreferenceRepository.
type PreferenceRepository struct {
mu sync.RWMutex
prefs map[string]*domain.UserPreferences
}
// NewPreferenceRepository creates a new in-memory preference repository.
func NewPreferenceRepository() *PreferenceRepository {
return &PreferenceRepository{
prefs: make(map[string]*domain.UserPreferences),
}
}
// Get returns the preferences for a user by ID.
// Returns nil, nil when no preferences exist.
func (r *PreferenceRepository) Get(_ context.Context, userID string) (*domain.UserPreferences, error) {
r.mu.RLock()
defer r.mu.RUnlock()
p, ok := r.prefs[userID]
if !ok {
return nil, nil
}
// Return a defensive copy
cp := *p
return &cp, nil
}
// Upsert creates or replaces preferences for a user.
func (r *PreferenceRepository) Upsert(_ context.Context, prefs *domain.UserPreferences) error {
r.mu.Lock()
defer r.mu.Unlock()
cp := *prefs
r.prefs[prefs.UserID] = &cp
return nil
}