diff options
Diffstat (limited to 'pkg/handler/index.go')
-rw-r--r-- | pkg/handler/index.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/pkg/handler/index.go b/pkg/handler/index.go new file mode 100644 index 0000000..611a544 --- /dev/null +++ b/pkg/handler/index.go @@ -0,0 +1,32 @@ +package handler + +import ( + "github.com/labstack/echo/v4" + "sh.org.ru/pkg/model" + "sh.org.ru/pkg/tpl" +) + +func (h *Handler) Index(c echo.Context) error { + p := &Pagination{} + if err := c.Bind(p); err != nil { + return err + } + + quotes := make([]model.Quote, 0, 20) + count, err := h.DB.NewSelect(). + Model((*model.Quote)(nil)). + Order("id DESC"). + Where("approved = ?", true). + Limit(20). + Offset(p.Page*20). + ScanAndCount(c.Request().Context(), "es) + if err != nil { + return err + } + + return tpl.Index(quotes, p.Page, count).Render(c.Request().Context(), c.Response()) +} + +type Pagination struct { + Page int `query:"page" default:"0"` +} |