feat: add pagination

This commit is contained in:
Yanislav Igonin 2021-09-10 02:17:45 +03:00
parent 8860c4f26c
commit 9b69eb46b7
6 changed files with 83 additions and 9 deletions

View File

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

11
main.go
View File

@ -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 {

View File

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

View File

@ -23,3 +23,9 @@ type File struct {
Ext string `json:"-"`
Size int `json:"size"`
}
type IndexPageData struct {
Threads []Post
PagesCount int
Page int
}

View File

@ -0,0 +1,3 @@
#pagesList {
flex-wrap: wrap
}

View File

@ -15,8 +15,8 @@
<div class="container">
{{ template "post-form" }}
<div class="row row-cols-auto gy-4">
{{range $Post := .}}
<div id="" class="row row-cols-auto gy-4 mb-4">
{{ range $Post := .Threads }}
<div class="col col-sm-6 col-md-4 col-lg-3">
<div class="card">
@ -39,6 +39,19 @@
{{ end }}
</div>
<div class="col col-12">
<nav aria-label="Page navigation">
<ul id="pagesList" class="pagination justify-content-center">
{{ $ActivePage := .Page }}
{{ range $i := Iterate .PagesCount }}
<li class="page-item {{ if (eq $i $ActivePage) }} active {{ end }}">
<a class="page-link" href="/?page={{ $i }}">{{ $i }}</a>
</li>
{{ end }}
</ul>
</nav>
</div>
</div>
</body>