feat: use getByUsernamePrefix to optimize findExistingSeedUser()

This commit is contained in:
2025-11-21 17:04:35 +01:00
parent 3ffd83b0fb
commit 989a61e7d5

View File

@@ -7,7 +7,6 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"os" "os"
"strings"
"sync" "sync"
"time" "time"
@@ -238,23 +237,11 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
} }
func findExistingSeedUser(userRepo repositories.UserRepository) (*database.User, error) { func findExistingSeedUser(userRepo repositories.UserRepository) (*database.User, error) {
users, err := userRepo.GetAll(100, 0) user, err := userRepo.GetByUsernamePrefix("seed_admin_")
if err != nil { if err != nil {
return nil, err
}
for _, user := range users {
if len(user.Username) >= 11 && user.Username[:11] == "seed_admin_" {
if len(user.Email) >= 13 && strings.HasSuffix(user.Email, "@goyco.local") {
emailPrefix := user.Email[:len(user.Email)-13]
if len(emailPrefix) >= 11 && emailPrefix[:11] == "seed_admin_" {
return &user, nil
}
}
}
}
return nil, fmt.Errorf("no existing seed user found") return nil, fmt.Errorf("no existing seed user found")
}
return user, nil
} }
func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (*database.User, error) { func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (*database.User, error) {
@@ -289,7 +276,6 @@ func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (
return nil, fmt.Errorf("failed to create seed user after %d attempts", maxRetries) return nil, fmt.Errorf("failed to create seed user after %d attempts", maxRetries)
} }
func getVoteCounts(voteRepo repositories.VoteRepository, postID uint) (int, int, error) { func getVoteCounts(voteRepo repositories.VoteRepository, postID uint) (int, int, error) {
return voteRepo.GetVoteCountsByPostID(postID) return voteRepo.GetVoteCountsByPostID(postID)
} }