From 5c7551b6ad810f8842cb98ce50b46544a2725219 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Mon, 30 Aug 2021 12:09:27 +0300 Subject: [PATCH] wip on db quries --- .gitignore | 1 + controllers/threads_controller.go | 6 +- main.go | 8 +- repositories/mocks.go | 49 +++++++++++ repositories/threads_repository.go | 132 ++++++++++++++++++----------- 5 files changed, 144 insertions(+), 52 deletions(-) create mode 100644 repositories/mocks.go diff --git a/.gitignore b/.gitignore index 266f6cd..dc806f3 100644 --- a/.gitignore +++ b/.gitignore @@ -16,6 +16,7 @@ .vscode/ tmp/ +__debug_bin micrach diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index b8188a8..30e0bd0 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -9,7 +9,11 @@ import ( ) func GetThreads(c *gin.Context) { - threads := Repositories.Threads.Get() + threads, err := Repositories.Threads.Get(10, 10) + if err != nil { + c.JSON(http.StatusOK, gin.H{"error": true}) + return + } c.HTML(http.StatusOK, "index.html", threads) } diff --git a/main.go b/main.go index 160cda1..7dbf4db 100644 --- a/main.go +++ b/main.go @@ -9,15 +9,17 @@ import ( Config "micrach/config" Controllers "micrach/controllers" - // Db "micrach/db" + Db "micrach/db" + Repositories "micrach/repositories" // 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() router := gin.Default() router.LoadHTMLGlob("templates/*.html") diff --git a/repositories/mocks.go b/repositories/mocks.go new file mode 100644 index 0000000..42d70c8 --- /dev/null +++ b/repositories/mocks.go @@ -0,0 +1,49 @@ +package repositories + +import "time" + +func getFile(id, postId int, name string) File { + return File{ + ID: id, + PostID: postId, + Name: name, + Ext: "image/jpeg", + Size: 10000, + CreatedAt: time.Now(), + } +} + +func getPost(id, threadID int) Post { + return Post{ + ID: id, + ThreadID: threadID, + Title: "Basic Title", + Text: "Basic Text", + IsSage: false, + 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(), + } +} + +func getThread(id int) Thread { + return Thread{ + ID: id, + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + Posts: []Post{ + getPost(1, id), + getPost(1, id), + }, + } +} + +var ThreadsDb = []Thread{} + +func SeedMocks() { + for i := 1; i < 101; i++ { + ThreadsDb = append(ThreadsDb, getThread(i)) + } +} diff --git a/repositories/threads_repository.go b/repositories/threads_repository.go index 22da0c3..fe7bd43 100644 --- a/repositories/threads_repository.go +++ b/repositories/threads_repository.go @@ -1,55 +1,91 @@ package repositories -import "time" - -func getFile(id, postId int, name string) File { - return File{ - ID: id, - PostID: postId, - Name: name, - Ext: "image/jpeg", - Size: 10000, - CreatedAt: time.Now(), - } -} - -func getPost(id, threadID int) Post { - return Post{ - ID: id, - ThreadID: threadID, - Title: "Basic Title", - Text: "Basic Text", - IsSage: false, - 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(), - } -} - -func getThread(id int) Thread { - return Thread{ - ID: id, - CreatedAt: time.Now(), - UpdatedAt: time.Now(), - Posts: []Post{ - getPost(1, id), - getPost(1, id), - }, - } -} - -var ThreadsDb = []Thread{ - getThread(1), - getThread(2), - getThread(3), -} - type ThreadsRepository struct{} var Threads ThreadsRepository -func (r *ThreadsRepository) Get() []Thread { - return ThreadsDb +func (r *ThreadsRepository) Get(limit, offset int) ([]Thread, 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 ThreadsDb, 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 +// }