mirror of
https://github.com/yanislav-igonin/micrach
synced 2024-12-22 22:32:33 +03:00
feat: add files seeds in db
This commit is contained in:
parent
c300def2cc
commit
1d93c5621d
@ -28,6 +28,9 @@ func getFile(id, postId int, name string) File {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Creates post mock with mock files in it
|
||||||
|
//
|
||||||
|
// id - post ID, pid - parent post ID.
|
||||||
func getPost(id int, pid *int) Post {
|
func getPost(id int, pid *int) Post {
|
||||||
var parentID int
|
var parentID int
|
||||||
if pid == nil {
|
if pid == nil {
|
||||||
@ -70,24 +73,48 @@ func seedLocal() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func seedDb() {
|
func seedDb() {
|
||||||
var posts []Post
|
// preparing seed data with parent posts with files
|
||||||
|
var parentPosts []Post
|
||||||
for i := 1; i < 10; i++ {
|
for i := 1; i < 10; i++ {
|
||||||
post := getPost(i, nil)
|
post := getPost(i, nil)
|
||||||
posts = append(posts, post)
|
parentPosts = append(parentPosts, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, parentPost := range posts {
|
for _, parentPost := range parentPosts {
|
||||||
for i := 0; i < 10; i++ {
|
// saving parent post in db
|
||||||
childPost := getPost(parentPost.ID*10+i, &parentPost.ID)
|
parentPostID, err := Posts.Create(parentPost)
|
||||||
posts = append(posts, childPost)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, post := range posts {
|
|
||||||
_, err := Posts.Create(post)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panicln(err)
|
log.Panicln(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// saving parent post files
|
||||||
|
for _, file := range parentPost.Files {
|
||||||
|
file.PostID = parentPostID
|
||||||
|
err = Files.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// making child posts
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
// getting child post with files
|
||||||
|
childPost := getPost(0, &parentPostID)
|
||||||
|
childPostID, err := Posts.Create(childPost)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// saving child post files
|
||||||
|
for _, file := range childPost.Files {
|
||||||
|
file.PostID = childPostID
|
||||||
|
err = Files.Create(file)
|
||||||
|
if err != nil {
|
||||||
|
log.Panicln(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user