From 9b69eb46b75e9022213f384052a356c92a3caec1 Mon Sep 17 00:00:00 2001 From: Yanislav Igonin Date: Fri, 10 Sep 2021 02:17:45 +0300 Subject: [PATCH] feat: add pagination --- controllers/threads_controller.go | 26 ++++++++++++++++++++++++-- main.go | 11 +++++++++++ repositories/posts_repository.go | 23 +++++++++++++++++++++-- repositories/structs.go | 6 ++++++ static/index.css | 3 +++ templates/index.html | 23 ++++++++++++++++++----- 6 files changed, 83 insertions(+), 9 deletions(-) diff --git a/controllers/threads_controller.go b/controllers/threads_controller.go index 2688aec..b919417 100644 --- a/controllers/threads_controller.go +++ b/controllers/threads_controller.go @@ -2,6 +2,7 @@ package controlers import ( "log" + "math" "net/http" "strconv" @@ -11,13 +12,34 @@ import ( ) func GetThreads(c *gin.Context) { - threads, err := Repositories.Posts.Get(10, 10) + pageString := c.Query("page") + page, err := strconv.Atoi(pageString) if err != nil { log.Println("error:", err) c.HTML(http.StatusOK, "500.html", nil) return } - c.HTML(http.StatusOK, "index.html", threads) + limit := 10 + offset := limit * (page - 1) + threads, err := Repositories.Posts.Get(limit, offset) + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusOK, "500.html", nil) + return + } + count, err := Repositories.Posts.GetCount() + if err != nil { + log.Println("error:", err) + c.HTML(http.StatusOK, "500.html", nil) + return + } + + data := Repositories.IndexPageData{ + Threads: threads, + PagesCount: int(math.Ceil(float64(count) / 10)), + Page: page, + } + c.HTML(http.StatusOK, "index.html", data) } func GetThread(c *gin.Context) { diff --git a/main.go b/main.go index 0df9446..b5a03a4 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "html/template" "log" "strconv" "time" @@ -41,6 +42,16 @@ func main() { middleware := mgin.NewMiddleware(instance) router := gin.Default() + router.SetFuncMap(template.FuncMap{ + "Iterate": func(count int) []int { + var i int + var Items []int + for i = 1; i < count; i++ { + Items = append(Items, i) + } + return Items + }, + }) router.LoadHTMLGlob("templates/*.html") router.ForwardedByClientIP = true if Config.App.IsRateLimiterEnabled { diff --git a/repositories/posts_repository.go b/repositories/posts_repository.go index 013d584..254c4d9 100644 --- a/repositories/posts_repository.go +++ b/repositories/posts_repository.go @@ -19,10 +19,11 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { is_parent = true AND is_deleted = false ORDER BY updated_at DESC - LIMIT $1 + OFFSET $1 + LIMIT $2 ` - rows, err := Db.Pool.Query(context.TODO(), sql, limit) + rows, err := Db.Pool.Query(context.TODO(), sql, offset, limit) if err != nil { return nil, err } @@ -58,6 +59,24 @@ func (r *PostsRepository) Get(limit, offset int) ([]Post, error) { return posts, nil } +func (r *PostsRepository) GetCount() (int, error) { + sql := ` + SELECT COUNT(*) + FROM posts + WHERE + is_parent = true + AND is_deleted = false + ` + + row := Db.Pool.QueryRow(context.TODO(), sql) + var count int + err := row.Scan(&count) + if err != nil { + return 0, err + } + return count, nil +} + func (r *PostsRepository) Create(p Post) (int, error) { sql := ` INSERT INTO posts (is_parent, parent_id, title, text, is_sage) diff --git a/repositories/structs.go b/repositories/structs.go index f8d3106..e019585 100644 --- a/repositories/structs.go +++ b/repositories/structs.go @@ -23,3 +23,9 @@ type File struct { Ext string `json:"-"` Size int `json:"size"` } + +type IndexPageData struct { + Threads []Post + PagesCount int + Page int +} diff --git a/static/index.css b/static/index.css index e69de29..00426ea 100644 --- a/static/index.css +++ b/static/index.css @@ -0,0 +1,3 @@ +#pagesList { + flex-wrap: wrap +} \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index fffd9ad..1e7bc6b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -15,19 +15,19 @@
{{ template "post-form" }} -
- {{range $Post := .}} +
+ {{ range $Post := .Threads }}
{{ $length := len $Post.Files }} {{ if gt $length 0 }} - {{$FirstFile := index $Post.Files 0}} + {{ $FirstFile := index $Post.Files 0 }} Uploaded picture - {{end}} + {{ end }}
{{$Post.Title}}
@@ -36,7 +36,20 @@
- {{end}} + {{ end }} +
+ +
+