aboutsummaryrefslogtreecommitdiff
path: root/pkg/handler
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/handler')
-rw-r--r--pkg/handler/add.go60
-rw-r--r--pkg/handler/admin.go57
-rw-r--r--pkg/handler/handler.go7
-rw-r--r--pkg/handler/index.go32
-rw-r--r--pkg/handler/quote.go27
-rw-r--r--pkg/handler/random.go18
6 files changed, 201 insertions, 0 deletions
diff --git a/pkg/handler/add.go b/pkg/handler/add.go
new file mode 100644
index 0000000..a6a5ced
--- /dev/null
+++ b/pkg/handler/add.go
@@ -0,0 +1,60 @@
+package handler
+
+import (
+ "net/http"
+
+ "github.com/labstack/echo/v4"
+ "github.com/ssoda/captcha"
+ "sh.org.ru/pkg/model"
+ "sh.org.ru/pkg/tpl"
+)
+
+func (h *Handler) AddQuote(c echo.Context) error {
+ cid := captcha.New()
+ form := &tpl.AddQuoteForm{
+ CaptchaID: cid,
+ }
+ return tpl.
+ AddQuotePage(form, "").
+ Render(c.Request().Context(), c.Response())
+}
+
+func (h *Handler) AddQuotePost(c echo.Context) error {
+ form := &tpl.AddQuoteForm{}
+ if err := c.Bind(form); err != nil {
+ return err
+ }
+ if form.CaptchaValue == "" {
+ return formError(form, c, "Неверный код")
+ }
+ if !captcha.VerifyString(form.CaptchaID, form.CaptchaValue) {
+ return formError(form, c, "Неверный код")
+ }
+
+ if len(form.Quote) < 10 {
+ return formError(form, c, "Цитата слишком короткая")
+ }
+
+ q := &model.Quote{
+ Quote: form.Quote,
+ Approved: false,
+ Archive: false,
+ }
+ if _, err := h.DB.NewInsert().Model(q).Exec(c.Request().Context()); err != nil {
+ return err
+ }
+
+ return c.Redirect(http.StatusFound, "/add/success")
+}
+
+func (h *Handler) AddQuoteSuccess(c echo.Context) error {
+ return tpl.AddQuoteSuccessPage().Render(c.Request().Context(), c.Response())
+}
+
+func formError(form *tpl.AddQuoteForm, c echo.Context, err string) error {
+ form.CaptchaID = captcha.New()
+ form.CaptchaValue = ""
+ return tpl.
+ AddQuotePage(form, err).
+ Render(c.Request().Context(), c.Response())
+}
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(), &quotes)
+ 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/")
+}
diff --git a/pkg/handler/handler.go b/pkg/handler/handler.go
new file mode 100644
index 0000000..5ba3966
--- /dev/null
+++ b/pkg/handler/handler.go
@@ -0,0 +1,7 @@
+package handler
+
+import "github.com/uptrace/bun"
+
+type Handler struct {
+ DB *bun.DB
+}
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(), &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"`
+}
diff --git a/pkg/handler/quote.go b/pkg/handler/quote.go
new file mode 100644
index 0000000..af9fd82
--- /dev/null
+++ b/pkg/handler/quote.go
@@ -0,0 +1,27 @@
+package handler
+
+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 err
+ }
+
+ 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/random.go b/pkg/handler/random.go
new file mode 100644
index 0000000..091058f
--- /dev/null
+++ b/pkg/handler/random.go
@@ -0,0 +1,18 @@
+package handler
+
+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(), &quotes)
+ if err != nil {
+ return err
+ }
+
+ return tpl.Random(quotes).Render(c.Request().Context(), c.Response())
+}