2024-10-08 03:43:08 +03:00
|
|
|
package add
|
2024-10-06 17:04:37 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2024-10-08 03:43:08 +03:00
|
|
|
if _, err := h.db.NewInsert().Model(q).Exec(c.Request().Context()); err != nil {
|
2024-10-06 17:04:37 +03:00
|
|
|
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())
|
|
|
|
}
|