2024-10-06 17:04:37 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2024-10-07 04:18:59 +03:00
|
|
|
"github.com/a-h/templ"
|
2024-10-06 17:04:37 +03:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-10-07 04:18:59 +03:00
|
|
|
comp := tpl.Random(quotes)
|
|
|
|
|
|
|
|
if c.Request().Header.Get("Hx-Request") == "true" {
|
|
|
|
return comp.Render(c.Request().Context(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := templ.WithChildren(c.Request().Context(), comp)
|
|
|
|
|
|
|
|
return tpl.Layout(tpl.HeaderParams{
|
|
|
|
Title: "Цитатник Рунета",
|
|
|
|
Description: "Новый цитатник Рунета",
|
|
|
|
URL: "https://sh.org.ru/",
|
|
|
|
}).Render(ctx, c.Response())
|
2024-10-06 17:04:37 +03:00
|
|
|
}
|