feat: seed user is now uniq
This commit is contained in:
@@ -236,26 +236,16 @@ func seedDatabase(userRepo repositories.UserRepository, postRepo repositories.Po
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findExistingSeedUser(userRepo repositories.UserRepository) (*database.User, error) {
|
const (
|
||||||
user, err := userRepo.GetByUsernamePrefix("seed_admin_")
|
seedUsername = "seed_admin"
|
||||||
if err != nil {
|
seedEmail = "seed_admin@goyco.local"
|
||||||
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) {
|
||||||
existingUser, err := findExistingSeedUser(userRepo)
|
if user, err := userRepo.GetByUsername(seedUsername); err == nil {
|
||||||
if err == nil && existingUser != nil {
|
return user, nil
|
||||||
return existingUser, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
randomID := generateRandomIdentifier()
|
|
||||||
seedUsername := fmt.Sprintf("seed_admin_%s", randomID)
|
|
||||||
seedEmail := fmt.Sprintf("seed_admin_%s@goyco.local", randomID)
|
|
||||||
|
|
||||||
const maxRetries = 10
|
|
||||||
for range maxRetries {
|
|
||||||
user := &database.User{
|
user := &database.User{
|
||||||
Username: seedUsername,
|
Username: seedUsername,
|
||||||
Email: seedEmail,
|
Email: seedEmail,
|
||||||
@@ -264,16 +254,10 @@ func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := userRepo.Create(user); err != nil {
|
if err := userRepo.Create(user); err != nil {
|
||||||
randomID = generateRandomIdentifier()
|
return nil, fmt.Errorf("failed to create seed user: %w", err)
|
||||||
seedUsername = fmt.Sprintf("seed_admin_%s", randomID)
|
|
||||||
seedEmail = fmt.Sprintf("seed_admin_%s@goyco.local", randomID)
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
Reference in New Issue
Block a user