refactor(services): Modernize Go code style in collection and filters services

Apply Go 1.18+ language features and modern style:

internal/services/collection_service.go:
- Use map[string]any instead of map[string]interface{} (Go 1.18+)
- Use range clause with single variable for iteration-only loops
- Replace if-else chains with switch statements for better readability
- Remove explicit type initialization for zero values

internal/services/filters.go:
- Add Err prefix to custom error variable for error naming convention

internal/router/library.go:
- Use cfg.ProcessingIssuesHandler instead of local processingIssuesHandler variable
- Ensures proper dependency injection through router config

These changes follow current Go best practices and improve code readability.
This commit is contained in:
2026-04-13 09:25:01 -04:00
parent 6287088bc1
commit e1aef8e85f
3 changed files with 12 additions and 11 deletions
+9 -8
View File
@@ -40,7 +40,7 @@ func NewCollectionService(db *database.Queries) *CollectionService {
}
// CreateCollection creates a new collection
func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]interface{}) (database.Collections, error) {
func (s *CollectionService) CreateCollection(ctx context.Context, userID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]any) (database.Collections, error) {
// Convert rules to JSONB ([]byte)
var rulesJSON []byte
if len(autoAssignRules) > 0 {
@@ -89,13 +89,13 @@ func (s *CollectionService) GetCollection(ctx context.Context, collectionID uuid
}
// GetCollectionWithBookCount gets a collection with book count
func (s *CollectionService) GetCollectionWithBookCount(ctx context.Context, collectionID uuid.UUID) (map[string]interface{}, error) {
func (s *CollectionService) GetCollectionWithBookCount(ctx context.Context, collectionID uuid.UUID) (map[string]any, error) {
collection, err := s.db.GetCollectionWithBookCount(ctx, pgtype.UUID{Bytes: collectionID, Valid: true})
if err != nil {
return nil, fmt.Errorf("failed to get collection: %v", err)
}
return map[string]interface{}{
return map[string]any{
"id": uuid.UUID(collection.ID.Bytes).String(),
"user_id": uuid.UUID(collection.UserID.Bytes).String(),
"name": collection.Name,
@@ -120,7 +120,7 @@ func (s *CollectionService) GetUserCollections(ctx context.Context, userID uuid.
}
// UpdateCollection updates a collection
func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]interface{}) (database.Collections, error) {
func (s *CollectionService) UpdateCollection(ctx context.Context, collectionID uuid.UUID, name, description, color, icon string, autoAssignRules []Rule, viewSettings map[string]any) (database.Collections, error) {
// Convert rules to JSONB ([]byte)
var rulesJSON []byte
if len(autoAssignRules) > 0 {
@@ -224,7 +224,7 @@ func (s *CollectionService) EvaluateRules(mediaItem database.ListMediaItemsRow,
// Sort rules by priority (higher priority first)
sortedRules := make([]Rule, len(rules))
copy(sortedRules, rules)
for i := 0; i < len(sortedRules); i++ {
for i := range len(sortedRules) {
for j := i + 1; j < len(sortedRules); j++ {
if sortedRules[i].Priority < sortedRules[j].Priority {
sortedRules[i], sortedRules[j] = sortedRules[j], sortedRules[i]
@@ -274,9 +274,10 @@ func (s *CollectionService) EvaluateRules(mediaItem database.ListMediaItemsRow,
// Calculate confidence based on rule type and priority
if eval.Matches {
baseConfidence := 0.7
if rule.Field == "genre" || rule.Field == "series" {
switch rule.Field {
case "genre", "series":
baseConfidence = 0.9
} else if rule.Field == "author" || rule.Field == "publisher" {
case "author", "publisher":
baseConfidence = 0.8
}
// Add priority boost (0.01 per priority point, max 0.1)
@@ -487,7 +488,7 @@ func parseFloat(s string) (float64, error) {
var result float64
var sign float64 = 1
var divisor float64 = 1
var decimalPlaces int = 0
var decimalPlaces = 0
var seenDecimal bool
// Skip leading whitespace
+1 -1
View File
@@ -11,7 +11,7 @@ import (
"github.com/jackc/pgx/v5/pgtype"
)
// Custom errors for saved filters
// ErrFilterNotFound - Custom errors for saved filters
var ErrFilterNotFound = errors.New("filter not found or access denied")
type FiltersService struct {