From b4a77d1b5583975407f9c2f1adf6b934a2cf74cf Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Tue, 31 Aug 2021 22:10:24 +0300 Subject: [PATCH] feat: add posts db seeds --- controllers/threads_controller.go | 2 +- main.go | 6 +- migrations/1-init.sql | 4 +- repositories/mocks.go | 78 +++++++++++++++++++++++-- repositories/posts_repository.go | 52 +++++++++++++++++ repositories/threads_repository.go | 91 ------------------------------ 6 files changed, 131 insertions(+), 102 deletions(-) create mode 100644 repositories/posts_repository.go delete mode 100644 repositories/threads_repository.go diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 30e0bd0..3ea083d 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -9,7 +9,7 @@ import ( ) func GetThreads(c *gin.Context) { - threads, err := Repositories.Threads.Get(10, 10) + threads, err := Repositories.Posts.Get(10, 10) if err != nil { c.JSON(http.StatusOK, gin.H{"error": true}) return diff --git a/main.go b/main.go index 8dc8123..70e434d 100644 --- a/main.go +++ b/main.go @@ -9,15 +9,15 @@ import ( Config "micrach/config" Controllers "micrach/controllers" + Db "micrach/db" Repositories "micrach/repositories" - // Db "micrach/db" // Utils "micrach/utils" ) func main() { Config.Init() - // Db.Init() - // defer Db.Pool.Close() + Db.Init() + defer Db.Pool.Close() gin.SetMode(Config.App.Env) Repositories.SeedMocks() diff --git a/migrations/1-init.sql b/migrations/1-init.sql index 255b747..4193ae3 100644 --- a/migrations/1-init.sql +++ b/migrations/1-init.sql @@ -5,9 +5,9 @@ CREATE TABLE posts id SERIAL 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, text TEXT NOT NULL, diff --git a/repositories/mocks.go b/repositories/mocks.go index 2599055..614e4a4 100644 --- a/repositories/mocks.go +++ b/repositories/mocks.go @@ -1,7 +1,9 @@ package repositories import ( + "log" "math/rand" + Db "micrach/db" "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{ ID: id, - IsParent: true, - ParentID: 0, + IsParent: isParent, + ParentID: parentID, IsDeleted: false, Title: randSeq(rand.Intn(100)), Text: randSeq(rand.Intn(100)), @@ -46,10 +61,63 @@ func getPost(id int) Post { var PostsDb = []Post{} -func SeedMocks() { +func seedLocalMocks() { rand.Seed(time.Now().UnixNano()) 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") +} diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go new file mode 100644 index 0000000..09488ac --- /dev/null +++ b/repositories/posts_repository.go @@ -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 { +// } diff --git a/repositories/threads_repository.go b/repositories/threads_repository.go deleted file mode 100644 index 7d0d7e0..0000000 --- a/repositories/threads_repository.go +++ /dev/null @@ -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 -// }