diff options
author | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-06 17:04:37 +0300 |
---|---|---|
committer | Alexander Neonxp Kiryukhin <i@neonxp.ru> | 2024-10-06 17:04:37 +0300 |
commit | 6160b4fdc5f37e4ceaa6b3c5acc855f466049d61 (patch) | |
tree | b6e41f9cad22515a61cc50b0a82d98ee55e0c3e3 /pkg/handler/admin.go | |
parent | 81b13617c4d0ca68afb181d1105386f0c339864d (diff) |
Первая версия
Diffstat (limited to 'pkg/handler/admin.go')
-rw-r--r-- | pkg/handler/admin.go | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/pkg/handler/admin.go b/pkg/handler/admin.go new file mode 100644 index 0000000..8531011 --- /dev/null +++ b/pkg/handler/admin.go @@ -0,0 +1,57 @@ +package handler + +import ( + "net/http" + + "github.com/labstack/echo/v4" + "sh.org.ru/pkg/model" + "sh.org.ru/pkg/tpl" +) + +func (h *Handler) Admin(c echo.Context) error { + quotes := make([]model.Quote, 0, 20) + count, err := h.DB.NewSelect(). + Model((*model.Quote)(nil)). + Order("id ASC"). + Where("approved = ?", false). + ScanAndCount(c.Request().Context(), "es) + if err != nil { + return err + } + + return tpl.Admin(quotes, count).Render(c.Request().Context(), c.Response()) +} + +func (h *Handler) AdminAction(c echo.Context) error { + form := new(tpl.AdminForm) + if err := c.Bind(form); err != nil { + return err + } + + switch form.Action { + case "approve": + _, err := h.DB.NewUpdate(). + Model(&model.Quote{ + ID: int64(form.ID), + Approved: true, + }). + Column("approved"). + WherePK("id"). + Exec(c.Request().Context()) + if err != nil { + return err + } + case "decline": + _, err := h.DB.NewDelete(). + Model(&model.Quote{ + ID: int64(form.ID), + }). + WherePK("id"). + Exec(c.Request().Context()) + if err != nil { + return err + } + } + + return c.Redirect(http.StatusFound, "/admin/") +} |