53 lines
1.7 KiB
Go
53 lines
1.7 KiB
Go
// Package service provides business logic / use cases for the application.
|
|
package service
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.threesix.ai/jordan/slate-test-1770505673/pkg/logging"
|
|
"git.threesix.ai/jordan/slate-test-1770505673/services/preferences-api/internal/domain"
|
|
"git.threesix.ai/jordan/slate-test-1770505673/services/preferences-api/internal/port"
|
|
)
|
|
|
|
// PreferenceService handles preference-related business logic.
|
|
type PreferenceService struct {
|
|
repo port.PreferenceRepository
|
|
logger *logging.Logger
|
|
}
|
|
|
|
// NewPreferenceService creates a new preference service.
|
|
func NewPreferenceService(repo port.PreferenceRepository, logger *logging.Logger) *PreferenceService {
|
|
return &PreferenceService{
|
|
repo: repo,
|
|
logger: logger.WithService("PreferenceService"),
|
|
}
|
|
}
|
|
|
|
// Get returns all preferences for a user.
|
|
func (s *PreferenceService) Get(ctx context.Context, userID string) (map[string]string, error) {
|
|
return s.repo.GetByUserID(ctx, userID)
|
|
}
|
|
|
|
// Upsert validates and persists preferences, then returns the full preference set.
|
|
func (s *PreferenceService) Upsert(ctx context.Context, userID string, prefs map[string]string) (map[string]string, error) {
|
|
// Validate all keys and values before persisting
|
|
for key, value := range prefs {
|
|
if err := domain.ValidateKey(key); err != nil {
|
|
return nil, fmt.Errorf("%w", err)
|
|
}
|
|
if err := domain.ValidateValue(key, value); err != nil {
|
|
return nil, fmt.Errorf("%w", err)
|
|
}
|
|
}
|
|
|
|
if err := s.repo.Upsert(ctx, userID, prefs); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.logger.Info("preferences updated", "user_id", userID, "keys_updated", len(prefs))
|
|
|
|
// Return the full preference set after update
|
|
return s.repo.GetByUserID(ctx, userID)
|
|
}
|