feat: add posts db seeds

This commit is contained in:
Yanislav Igonin 2021-08-31 22:10:24 +03:00
parent 872a6b58df
commit b4a77d1b55
6 changed files with 131 additions and 102 deletions

View File

@ -9,7 +9,7 @@ import (
) )
func GetThreads(c *gin.Context) { func GetThreads(c *gin.Context) {
threads, err := Repositories.Threads.Get(10, 10) threads, err := Repositories.Posts.Get(10, 10)
if err != nil { if err != nil {
c.JSON(http.StatusOK, gin.H{"error": true}) c.JSON(http.StatusOK, gin.H{"error": true})
return return

View File

@ -9,15 +9,15 @@ import (
Config "micrach/config" Config "micrach/config"
Controllers "micrach/controllers" Controllers "micrach/controllers"
Db "micrach/db"
Repositories "micrach/repositories" Repositories "micrach/repositories"
// Db "micrach/db"
// Utils "micrach/utils" // Utils "micrach/utils"
) )
func main() { func main() {
Config.Init() Config.Init()
// Db.Init() Db.Init()
// defer Db.Pool.Close() defer Db.Pool.Close()
gin.SetMode(Config.App.Env) gin.SetMode(Config.App.Env)
Repositories.SeedMocks() Repositories.SeedMocks()

View File

@ -5,9 +5,9 @@ CREATE TABLE posts
id SERIAL NOT NULL, id SERIAL NOT NULL,
is_parent BOOLEAN NOT NULL, is_parent BOOLEAN NOT NULL,
parent_id INT REFERENCES posts (id), parent_id INT REFERENCES posts (id) NULL,
is_deleted BOOLEAN NOT NULL, is_deleted BOOLEAN DEFAULT false NOT NULL,
title VARCHAR NOT NULL, title VARCHAR NOT NULL,
text TEXT NOT NULL, text TEXT NOT NULL,

View File

@ -1,7 +1,9 @@
package repositories package repositories
import ( import (
"log"
"math/rand" "math/rand"
Db "micrach/db"
"time" "time"
) )
@ -26,11 +28,24 @@ func getFile(id, postId int, name string) File {
} }
} }
func getPost(id int) Post { 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
}
return Post{ return Post{
ID: id, ID: id,
IsParent: true, IsParent: isParent,
ParentID: 0, ParentID: parentID,
IsDeleted: false, IsDeleted: false,
Title: randSeq(rand.Intn(100)), Title: randSeq(rand.Intn(100)),
Text: randSeq(rand.Intn(100)), Text: randSeq(rand.Intn(100)),
@ -46,10 +61,63 @@ func getPost(id int) Post {
var PostsDb = []Post{} var PostsDb = []Post{}
func SeedMocks() { func seedLocalMocks() {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
for i := 1; i < 10; i++ { for i := 1; i < 10; i++ {
PostsDb = append(PostsDb, getPost(i)) 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()
}
log.Println("mocks - online")
}

View File

@ -0,0 +1,52 @@
package repositories
import (
"context"
Db "micrach/db"
"github.com/jackc/pgx/v4"
)
type PostsRepository struct{}
var Posts PostsRepository
func (r *PostsRepository) Get(limit, offset int) ([]Post, error) {
return PostsDb, nil
}
func (r *PostsRepository) Create(p Post) (int, error) {
sql := `
INSERT INTO posts (is_parent, parent_id, title, text, is_sage)
VALUES ($1, $2, $3, $4, $5)
RETURNING id
`
conn, err := Db.Pool.Acquire(context.TODO())
if err != nil {
return 0, err
}
defer conn.Release()
var row pgx.Row
if p.IsParent {
row = conn.QueryRow(
context.TODO(), sql, p.IsParent, nil, p.Title, p.Text, p.IsSage,
)
} else {
row = conn.QueryRow(
context.TODO(), sql, p.IsParent, p.ParentID, p.Title, p.Text, p.IsSage,
)
}
if err != nil {
return 0, nil
}
createdPost := new(Post)
row.Scan(&createdPost.ID)
return createdPost.ID, nil
}
// func (r *PostsRepository) GetByID() int {
// }

View File

@ -1,91 +0,0 @@
package repositories
type ThreadsRepository struct{}
var Threads ThreadsRepository
func (r *ThreadsRepository) Get(limit, offset int) ([]Post, error) {
// conn, err := Db.Pool.Acquire(context.TODO())
// if err != nil {
// return nil, err
// }
// defer conn.Release()
// sql := `
// SELECT
// id
// FROM threads
// WHERE is_deleted != true
// ORDER BY updated_at DESC
// `
// rows, err := conn.Query(context.TODO(), sql)
// if err != nil {
// return nil, err
// }
// var threads []Thread
// var threadsIDs []int
// for rows.Next() {
// var thread Thread
// err = rows.Scan(&thread.ID)
// if err != nil {
// return nil, err
// }
// threadsIDs = append(threadsIDs, thread.ID)
// threads = append(threads, thread)
// }
// rows.Close()
// sql = `
// SELECT
// id,
// thread_id,
// title,
// text,
// is_sage,
// created_at
// FROM posts
// WHERE thread_id IN (
// `
// buf := bytes.NewBufferString(sql)
// for i, tid := range threadsIDs {
// buf.WriteString(strconv.Itoa(tid))
// if i != len(threadsIDs)-1 {
// buf.WriteString(",")
// }
// }
// sql = buf.String() + ") ORDER BY created_at ASC"
// rows, err = conn.Query(context.TODO(), sql)
// if err != nil {
// return nil, err
// }
// var posts []Post
// var postIDs []int
// for rows.Next() {
// var post Post
// err = rows.Scan(&post.ID, &post.ThreadID, &post.Title, &post.Text, &post.IsSage, &post.CreatedAt)
// if err != nil {
// return nil, err
// }
// postIDs = append(postIDs, post.ID)
// posts = append(posts, post)
// }
// rows.Close()
return PostsDb, nil
// return nil, nil
}
// func (r *ThreadsRepository) Create(post Post) int {
// }
// func (r *ThreadsRepository) GetByID() int {
// newThreadID := time.Now().Second()
// ThreadsDb = append(ThreadsDb, getThread(newThreadID))
// return newThreadID
// }