slate-v3-1770514618/services/preferences-api/internal/adapter/memory/preferences.go
rdev-worker 1afe983cd6
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
build: /implement-feature user-preferences
2026-02-08 02:02:18 +00:00

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
}