clean: remove duplicate sequential helpers
This commit is contained in:
@@ -299,180 +299,6 @@ func ensureSeedUser(userRepo repositories.UserRepository, passwordHash string) (
|
||||
return nil, fmt.Errorf("failed to create seed user after %d attempts", maxRetries)
|
||||
}
|
||||
|
||||
func createRandomUsers(userRepo repositories.UserRepository, count int, passwordHash string) ([]database.User, error) {
|
||||
var users []database.User
|
||||
|
||||
for i := range count {
|
||||
username := fmt.Sprintf("user_%d", i+1)
|
||||
email := fmt.Sprintf("user_%d@goyco.local", i+1)
|
||||
|
||||
user := &database.User{
|
||||
Username: username,
|
||||
Email: email,
|
||||
Password: passwordHash,
|
||||
EmailVerified: true,
|
||||
}
|
||||
|
||||
if err := userRepo.Create(user); err != nil {
|
||||
return nil, fmt.Errorf("create user %d: %w", i+1, err)
|
||||
}
|
||||
|
||||
users = append(users, *user)
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func createRandomPosts(postRepo repositories.PostRepository, authorID uint, count int) ([]database.Post, error) {
|
||||
var posts []database.Post
|
||||
|
||||
sampleTitles := []string{
|
||||
"Amazing JavaScript Framework",
|
||||
"Python Best Practices",
|
||||
"Go Performance Tips",
|
||||
"Database Optimization",
|
||||
"Web Security Guide",
|
||||
"Machine Learning Basics",
|
||||
"Cloud Architecture",
|
||||
"DevOps Automation",
|
||||
"API Design Patterns",
|
||||
"Frontend Optimization",
|
||||
"Backend Scaling",
|
||||
"Container Orchestration",
|
||||
"Microservices Architecture",
|
||||
"Testing Strategies",
|
||||
"Code Review Process",
|
||||
"Version Control Best Practices",
|
||||
"Continuous Integration",
|
||||
"Monitoring and Alerting",
|
||||
"Error Handling Patterns",
|
||||
"Data Structures Explained",
|
||||
}
|
||||
|
||||
sampleDomains := []string{
|
||||
"example.com",
|
||||
"techblog.org",
|
||||
"devguide.net",
|
||||
"programming.io",
|
||||
"codeexamples.com",
|
||||
"tutorialhub.org",
|
||||
"bestpractices.dev",
|
||||
"learnprogramming.net",
|
||||
"codingtips.org",
|
||||
"softwareengineering.com",
|
||||
}
|
||||
|
||||
for i := range count {
|
||||
title := sampleTitles[i%len(sampleTitles)]
|
||||
if i >= len(sampleTitles) {
|
||||
title = fmt.Sprintf("%s - Part %d", title, (i/len(sampleTitles))+1)
|
||||
}
|
||||
|
||||
domain := sampleDomains[i%len(sampleDomains)]
|
||||
path := generateRandomPath()
|
||||
url := fmt.Sprintf("https://%s%s", domain, path)
|
||||
|
||||
content := fmt.Sprintf("Autogenerated seed post #%d\n\nThis is sample content for testing purposes. The post discusses %s and provides valuable insights.", i+1, title)
|
||||
|
||||
post := &database.Post{
|
||||
Title: title,
|
||||
URL: url,
|
||||
Content: content,
|
||||
AuthorID: &authorID,
|
||||
UpVotes: 0,
|
||||
DownVotes: 0,
|
||||
Score: 0,
|
||||
}
|
||||
|
||||
if err := postRepo.Create(post); err != nil {
|
||||
return nil, fmt.Errorf("create post %d: %w", i+1, err)
|
||||
}
|
||||
|
||||
posts = append(posts, *post)
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func generateRandomPath() string {
|
||||
initSeedRand()
|
||||
pathLength := seedRandSource.Intn(20)
|
||||
path := "/article/"
|
||||
|
||||
for i := 0; i < pathLength+5; i++ {
|
||||
randomChar := seedRandSource.Intn(26)
|
||||
path += string(rune('a' + randomChar))
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
func createRandomVotes(voteRepo repositories.VoteRepository, users []database.User, posts []database.Post, avgVotesPerPost int) (int, error) {
|
||||
initSeedRand()
|
||||
totalVotes := 0
|
||||
|
||||
for _, post := range posts {
|
||||
numVotes := seedRandSource.Intn(avgVotesPerPost*2 + 1)
|
||||
|
||||
if numVotes == 0 && avgVotesPerPost > 0 {
|
||||
if seedRandSource.Intn(5) > 0 {
|
||||
numVotes = 1
|
||||
}
|
||||
}
|
||||
|
||||
usedUsers := make(map[uint]bool)
|
||||
for i := 0; i < numVotes && len(usedUsers) < len(users); i++ {
|
||||
userIdx := seedRandSource.Intn(len(users))
|
||||
user := users[userIdx]
|
||||
|
||||
if usedUsers[user.ID] {
|
||||
continue
|
||||
}
|
||||
usedUsers[user.ID] = true
|
||||
|
||||
voteTypeInt := seedRandSource.Intn(10)
|
||||
var voteType database.VoteType
|
||||
if voteTypeInt < 7 {
|
||||
voteType = database.VoteUp
|
||||
} else {
|
||||
voteType = database.VoteDown
|
||||
}
|
||||
|
||||
vote := &database.Vote{
|
||||
UserID: &user.ID,
|
||||
PostID: post.ID,
|
||||
Type: voteType,
|
||||
}
|
||||
|
||||
if err := voteRepo.Create(vote); err != nil {
|
||||
return totalVotes, fmt.Errorf("create vote for post %d: %w", post.ID, err)
|
||||
}
|
||||
|
||||
totalVotes++
|
||||
}
|
||||
}
|
||||
|
||||
return totalVotes, nil
|
||||
}
|
||||
|
||||
func updatePostScores(postRepo repositories.PostRepository, voteRepo repositories.VoteRepository, posts []database.Post) error {
|
||||
for _, post := range posts {
|
||||
upVotes, downVotes, err := getVoteCounts(voteRepo, post.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get vote counts for post %d: %w", post.ID, err)
|
||||
}
|
||||
|
||||
post.UpVotes = upVotes
|
||||
post.DownVotes = downVotes
|
||||
post.Score = upVotes - downVotes
|
||||
|
||||
if err := postRepo.Update(&post); err != nil {
|
||||
return fmt.Errorf("update post %d scores: %w", post.ID, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getVoteCounts(voteRepo repositories.VoteRepository, postID uint) (int, int, error) {
|
||||
votes, err := voteRepo.GetByPostID(postID)
|
||||
|
||||
Reference in New Issue
Block a user