2021-08-30 12:09:27 +03:00
|
|
|
package repositories
|
|
|
|
|
2021-08-31 19:18:27 +03:00
|
|
|
import (
|
2021-08-31 22:10:24 +03:00
|
|
|
"log"
|
2021-08-31 19:18:27 +03:00
|
|
|
"math/rand"
|
2021-08-31 22:10:24 +03:00
|
|
|
Db "micrach/db"
|
2021-08-31 19:18:27 +03:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
|
|
|
|
|
|
|
func randSeq(n int) string {
|
|
|
|
b := make([]rune, n)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|
2021-08-30 12:09:27 +03:00
|
|
|
|
|
|
|
func getFile(id, postId int, name string) File {
|
|
|
|
return File{
|
|
|
|
ID: id,
|
|
|
|
PostID: postId,
|
|
|
|
Name: name,
|
|
|
|
Ext: "image/jpeg",
|
|
|
|
Size: 10000,
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 22:10:24 +03:00
|
|
|
func getPost(id int, pid *int) Post {
|
|
|
|
var parentID int
|
|
|
|
if pid == nil {
|
|
|
|
parentID = 0
|
|
|
|
} else {
|
|
|
|
parentID = *pid
|
|
|
|
}
|
|
|
|
|
|
|
|
var isParent bool
|
|
|
|
if parentID == 0 {
|
|
|
|
isParent = true
|
|
|
|
} else {
|
|
|
|
isParent = false
|
|
|
|
}
|
2021-08-30 12:09:27 +03:00
|
|
|
return Post{
|
2021-08-31 19:48:02 +03:00
|
|
|
ID: id,
|
2021-08-31 22:10:24 +03:00
|
|
|
IsParent: isParent,
|
|
|
|
ParentID: parentID,
|
2021-08-31 19:48:02 +03:00
|
|
|
IsDeleted: false,
|
|
|
|
Title: randSeq(rand.Intn(100)),
|
|
|
|
Text: randSeq(rand.Intn(100)),
|
|
|
|
IsSage: false,
|
2021-08-30 12:09:27 +03:00
|
|
|
Files: []File{
|
|
|
|
getFile(2, id, "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png"),
|
|
|
|
getFile(1, id, "https://memepedia.ru/wp-content/uploads/2018/03/ebanyy-rot-etogo-kazino.png"),
|
|
|
|
},
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-31 19:48:02 +03:00
|
|
|
var PostsDb = []Post{}
|
2021-08-30 12:09:27 +03:00
|
|
|
|
2021-08-31 22:10:24 +03:00
|
|
|
func seedLocalMocks() {
|
2021-08-31 19:18:27 +03:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2021-08-31 19:48:02 +03:00
|
|
|
for i := 1; i < 10; i++ {
|
2021-08-31 22:10:24 +03:00
|
|
|
PostsDb = append(PostsDb, getPost(i, nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func seedDbMocks() {
|
|
|
|
var posts []Post
|
|
|
|
for i := 1; i < 10; i++ {
|
|
|
|
post := getPost(i, nil)
|
|
|
|
posts = append(posts, post)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, parentPost := range posts {
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
childPost := getPost(parentPost.ID*10+i, &parentPost.ID)
|
|
|
|
posts = append(posts, childPost)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, post := range posts {
|
|
|
|
Posts.Create(post)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fileSql := `
|
|
|
|
// INSERT INTO files (post_id, name, ext, size)
|
|
|
|
// VALUES ($1, $2, $3, $4)
|
|
|
|
// `
|
|
|
|
// for _, post := range posts {
|
|
|
|
// if post.ParentID == 0 {
|
|
|
|
// conn.Query(context.Background(), postSql, post.ID, post.IsParent, nil, post.Title, post.Text, post.IsSage)
|
|
|
|
// } else {
|
|
|
|
// conn.Query(context.Background(), postSql, post.ID, post.IsParent, post.ParentID, post.Title, post.Text, post.IsSage)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Panicln(err)
|
|
|
|
// }
|
|
|
|
|
|
|
|
// for _, file := range post.Files {
|
|
|
|
// _, err = Db.Pool.Query(context.TODO(), fileSql, file.PostID, file.Name, file.Ext, file.Size)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Panicln(err)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func SeedMocks() {
|
|
|
|
if Db.Pool != nil {
|
|
|
|
seedDbMocks()
|
|
|
|
} else {
|
|
|
|
seedLocalMocks()
|
2021-08-30 12:09:27 +03:00
|
|
|
}
|
2021-08-31 22:10:24 +03:00
|
|
|
log.Println("mocks - online")
|
2021-08-30 12:09:27 +03:00
|
|
|
}
|