diff options
Diffstat (limited to 'pkg/handler/quote')
-rw-r--r-- | pkg/handler/quote/handler.go | 26 | ||||
-rw-r--r-- | pkg/handler/quote/index.go | 28 | ||||
-rw-r--r-- | pkg/handler/quote/quote.go | 27 | ||||
-rw-r--r-- | pkg/handler/quote/random.go | 18 | ||||
-rw-r--r-- | pkg/handler/quote/top.go | 28 |
5 files changed, 127 insertions, 0 deletions
diff --git a/pkg/handler/quote/handler.go b/pkg/handler/quote/handler.go new file mode 100644 index 0000000..04807c0 --- /dev/null +++ b/pkg/handler/quote/handler.go @@ -0,0 +1,26 @@ +package quote + +import ( + "github.com/labstack/echo/v4" + "github.com/uptrace/bun" +) + +type Handler struct { + db *bun.DB +} + +// NewHandler returns new Handler. +func NewHandler(db *bun.DB) *Handler { + return &Handler{db: db} +} + +func (h *Handler) Register(g *echo.Group) { + g.GET("", h.Index) + g.GET("quote/:id", h.Quote) + g.GET("random", h.Random) + g.GET("top", h.Top) +} + +type Pagination struct { + Page int `query:"page" default:"0"` +} diff --git a/pkg/handler/quote/index.go b/pkg/handler/quote/index.go new file mode 100644 index 0000000..9dd21f8 --- /dev/null +++ b/pkg/handler/quote/index.go @@ -0,0 +1,28 @@ +package quote + +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.List(quotes, p.Page, count).Render(c.Request().Context(), c.Response()) +} diff --git a/pkg/handler/quote/quote.go b/pkg/handler/quote/quote.go new file mode 100644 index 0000000..b25eb90 --- /dev/null +++ b/pkg/handler/quote/quote.go @@ -0,0 +1,27 @@ +package quote + +import ( + "strconv" + + "github.com/labstack/echo/v4" + "sh.org.ru/pkg/model" + "sh.org.ru/pkg/tpl" +) + +func (h *Handler) Quote(c echo.Context) error { + sid := c.Param("id") + id, err := strconv.Atoi(sid) + if err != nil { + return echo.ErrNotFound + } + + quote := new(model.Quote) + err = h.db.NewSelect(). + Model(quote). + Where("id = ?", id).Scan(c.Request().Context(), quote) + if err != nil { + return err + } + + return tpl.QuotePage(quote).Render(c.Request().Context(), c.Response()) +} diff --git a/pkg/handler/quote/random.go b/pkg/handler/quote/random.go new file mode 100644 index 0000000..1e04ff0 --- /dev/null +++ b/pkg/handler/quote/random.go @@ -0,0 +1,18 @@ +package quote + +import ( + "github.com/labstack/echo/v4" + "sh.org.ru/pkg/model" + "sh.org.ru/pkg/tpl" +) + +func (h *Handler) Random(c echo.Context) error { + quotes := make([]model.Quote, 0, 20) + err := h.db.NewRaw(`select q.* from quotes q where q.approved = true order by random() limit 20`). + Scan(c.Request().Context(), "es) + if err != nil { + return err + } + + return tpl.Random(quotes).Render(c.Request().Context(), c.Response()) +} diff --git a/pkg/handler/quote/top.go b/pkg/handler/quote/top.go new file mode 100644 index 0000000..c2803ea --- /dev/null +++ b/pkg/handler/quote/top.go @@ -0,0 +1,28 @@ +package quote + +import ( + "github.com/labstack/echo/v4" + "sh.org.ru/pkg/model" + "sh.org.ru/pkg/tpl" +) + +func (h *Handler) Top(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("rating DESC"). + Where("approved = ?", true). + Limit(20). + Offset(p.Page*20). + ScanAndCount(c.Request().Context(), "es) + if err != nil { + return err + } + + return tpl.List(quotes, p.Page, count).Render(c.Request().Context(), c.Response()) +} |