wip on db quries

This commit is contained in:
Yanislav Igonin 2021-08-30 12:09:27 +03:00
parent 1c2b8b83f1
commit 5c7551b6ad
5 changed files with 144 additions and 52 deletions

1
.gitignore vendored
View File

@ -16,6 +16,7 @@
.vscode/
tmp/
__debug_bin
micrach

View File

@ -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)
}

View File

@ -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")

49
repositories/mocks.go Normal file
View File

@ -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))
}
}

View File

@ -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
// }