Skip to content

Commit 1e79a7d

Browse files
committed
feat: add post seeder
1 parent 2647f29 commit 1e79a7d

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

migrations/json/posts.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
"text": "Your heart is the size of an ocean. Go find yourself in its hidden depths."
44
},
55
{
6-
"text": "The Bay of Bengal is hit frequently by cyclones. The months of November and May, in particular, are dangerous in this regard."
6+
"text": "The Bay of Bengal is hit frequently by cyclones. The months of November and May, in particular, are dangerous in this regard.",
7+
"parent_id": 1
78
},
89
{
910
"text": "Thinking is the capital, Enterprise is the way, Hard Work is the solution."
1011
},
1112
{
12-
"text": "Life is a gamble. You can get hurt, but people die in plane crashes, lose their arms and legs in car accidents; people die every day. Same with fighters: some die, some get hurt, some go on. You just don't let yourself believe it will happen to you."
13+
"text": "Life is a gamble. You can get hurt, but people die in plane crashes, lose their arms and legs in car accidents; people die every day. Same with fighters: some die, some get hurt, some go on. You just don't let yourself believe it will happen to you.",
14+
"parent_id": 1
1315
}
1416
]

migrations/seeder.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@ func Seeder(db *gorm.DB) error {
1010
return err
1111
}
1212

13+
if err := seeds.ListPostSeeder(db); err != nil {
14+
return err
15+
}
16+
1317
return nil
1418
}

migrations/seeds/post_seed.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package seeds
22

33
import (
44
"encoding/json"
5+
"errors"
56
"io"
67
"os"
78

@@ -31,9 +32,42 @@ func ListPostSeeder(db *gorm.DB) error {
3132

3233
var user entity.User
3334
err = db.Find(&user).Limit(2).Error
34-
if err != nil {
35+
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
36+
return err
37+
}
38+
39+
var users []entity.User
40+
err = db.Find(&users).Limit(2).Error
41+
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
3542
return err
3643
}
3744

45+
postMap := make(map[int]*entity.Post)
46+
47+
for i, post := range listPost {
48+
newPost := entity.Post{
49+
Text: post.Text,
50+
UserID: users[i%len(users)].ID,
51+
}
52+
53+
if err := db.Create(&newPost).Error; err != nil {
54+
return err
55+
}
56+
postMap[i] = &newPost
57+
}
58+
59+
for i, post := range listPost {
60+
if post.ParentID != nil {
61+
parentPost := postMap[int(*post.ParentID-1)]
62+
if parentPost != nil {
63+
currentPost := postMap[i]
64+
currentPost.ParentID = &parentPost.ID
65+
if err := db.Save(currentPost).Error; err != nil {
66+
return err
67+
}
68+
}
69+
}
70+
}
71+
3872
return nil
3973
}

0 commit comments

Comments
 (0)