2023-05-23 04:36:04 +04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2023-05-27 20:56:32 +04:00
|
|
|
"git.arav.su/Arav/httpr"
|
2023-08-12 22:58:54 +04:00
|
|
|
captcha "git.arav.su/Arav/justcaptcha/v2"
|
|
|
|
"git.arav.su/Arav/justcaptcha/v2/inmemdb"
|
2023-05-23 04:36:04 +04:00
|
|
|
"git.arav.su/Arav/justguestbook"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GuestbookApiHandlers struct {
|
|
|
|
Owner string
|
|
|
|
PageSize int64
|
|
|
|
db justguestbook.Guestbook
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGuestbookApiHandlers(owner string, pageSz int64, db justguestbook.Guestbook) *GuestbookApiHandlers {
|
|
|
|
return &GuestbookApiHandlers{Owner: owner, PageSize: pageSz, db: db}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) New(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var entry *justguestbook.Entry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
if !inmemdb.Solve(captcha.ID(r.FormValue("captcha_id")), captcha.Answer(r.FormValue("captcha_answer"))) {
|
2023-08-12 23:51:26 +04:00
|
|
|
Error(w, http.StatusForbidden, "Wrong answer given.",
|
|
|
|
"Here's your message:"+r.FormValue("message"), r.Referer())
|
2023-05-23 04:36:04 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.FormValue("name") == "" {
|
|
|
|
r.Form.Set("name", "Anonymous")
|
|
|
|
}
|
|
|
|
|
2023-05-28 04:52:09 +04:00
|
|
|
message := cleanupNewlines(r.FormValue("message"))
|
2023-05-23 04:36:04 +04:00
|
|
|
|
|
|
|
entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
|
|
|
|
r.FormValue("website"), r.FormValue("hide_website") != "")
|
|
|
|
if err != nil {
|
2023-08-12 23:51:26 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(),
|
|
|
|
"Here's your message:"+r.FormValue("message"), r.Referer())
|
2023-05-23 04:36:04 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.db.NewEntry(entry); err != nil {
|
2023-08-12 23:51:26 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(),
|
|
|
|
"Here's your message:"+r.FormValue("message"), r.Referer())
|
2023-05-23 04:36:04 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/guestbook", http.StatusMovedPermanently)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) Edit(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var entry *justguestbook.Entry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
if r.FormValue("name") == "" {
|
|
|
|
r.Form.Set("name", "Anonymous")
|
|
|
|
}
|
|
|
|
|
2023-05-28 04:52:09 +04:00
|
|
|
message := cleanupNewlines(r.FormValue("message"))
|
2023-05-23 04:36:04 +04:00
|
|
|
|
|
|
|
entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
|
|
|
|
r.FormValue("website"), r.FormValue("hide_website") != "")
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-27 20:56:32 +04:00
|
|
|
entry.ID, _ = strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.db.EditEntry(entry); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
if err := h.db.DeleteEntry(id); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) Reply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var reply *justguestbook.Reply
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
|
|
|
r.ParseForm()
|
|
|
|
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
|
2023-05-28 04:52:09 +04:00
|
|
|
message := cleanupNewlines(r.FormValue("message"))
|
2023-05-23 04:36:04 +04:00
|
|
|
|
|
|
|
reply, err = justguestbook.NewReply(id, message)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.db.NewReply(reply); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) EditReply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var reply *justguestbook.Reply
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
|
|
|
r.ParseForm()
|
|
|
|
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
|
2023-05-28 04:52:09 +04:00
|
|
|
message := cleanupNewlines(r.FormValue("message"))
|
2023-05-23 04:36:04 +04:00
|
|
|
|
|
|
|
reply, err = justguestbook.NewReply(id, message)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.db.EditReply(reply); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookApiHandlers) DeleteReply(w http.ResponseWriter, r *http.Request) {
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
if err := h.db.DeleteReply(id); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|