shorgru/pkg/handler/index.go

33 lines
643 B
Go
Raw Normal View History

2024-10-06 17:04:37 +03:00
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(), &quotes)
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"`
}