2022-10-19 03:25:43 +04:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-10-21 03:22:57 +04:00
|
|
|
"fmt"
|
2022-10-19 03:25:43 +04:00
|
|
|
"justguestbook/internal/guestbook"
|
2022-10-21 03:03:05 +04:00
|
|
|
"justguestbook/pkg/justcaptcha"
|
2022-10-19 03:25:43 +04:00
|
|
|
"justguestbook/pkg/server"
|
2022-10-21 03:03:05 +04:00
|
|
|
"log"
|
2022-10-19 03:25:43 +04:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GuestbookHandlers struct {
|
|
|
|
owner string
|
|
|
|
password string
|
|
|
|
anonymousName string
|
|
|
|
defaultPageSize int64
|
|
|
|
db guestbook.Guestbook
|
|
|
|
captchaAddr string
|
|
|
|
}
|
|
|
|
|
2022-10-21 03:03:05 +04:00
|
|
|
func New(owner, password, anonymousName string, defaultPageSize int64, guestbook guestbook.Guestbook, captchaAddr string) *GuestbookHandlers {
|
2022-10-19 03:25:43 +04:00
|
|
|
return &GuestbookHandlers{
|
|
|
|
owner: owner,
|
|
|
|
password: password,
|
|
|
|
anonymousName: anonymousName,
|
|
|
|
defaultPageSize: defaultPageSize,
|
|
|
|
db: guestbook,
|
2022-10-21 03:03:05 +04:00
|
|
|
captchaAddr: captchaAddr}
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
var page_num int64 = 1
|
|
|
|
var page_size int64 = h.defaultPageSize
|
|
|
|
|
|
|
|
if r.URL.Query().Get("p") != "" {
|
2022-10-21 03:03:05 +04:00
|
|
|
page_num, err = strconv.ParseInt(r.URL.Query().Get("p"), 10, 64)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
|
|
|
page_num = 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.URL.Query().Get("ps") != "" {
|
2022-10-21 03:03:05 +04:00
|
|
|
page_size, err = strconv.ParseInt(r.URL.Query().Get("ps"), 10, 64)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
|
|
|
page_size = h.defaultPageSize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := h.db.Entries(page_num, page_size)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-10-21 03:22:57 +04:00
|
|
|
log.Println("failed to retrieve entries:", err)
|
|
|
|
return
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
guestbookEntries := struct {
|
|
|
|
Owner string `json:"owner"`
|
|
|
|
Entries []*guestbook.Entry `json:"entries"`
|
|
|
|
}{
|
|
|
|
Owner: h.owner,
|
|
|
|
Entries: entries}
|
|
|
|
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2022-10-21 03:22:57 +04:00
|
|
|
if err := json.NewEncoder(w).Encode(&guestbookEntries); err != nil {
|
|
|
|
log.Println("failed to encode entries:", err)
|
|
|
|
http.Error(w, fmt.Sprintln("failed to encode entries:", err.Error()), http.StatusInternalServerError)
|
|
|
|
}
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var entry *guestbook.Entry
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
if r.FormValue("captcha_id") == "" {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
solved, err := justcaptcha.CheckCaptcha(r.FormValue("captcha_id"), h.captchaAddr)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("justcaptcha:", err)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !solved {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
name := r.FormValue("name")
|
|
|
|
if name == "" {
|
|
|
|
name = h.anonymousName
|
|
|
|
}
|
|
|
|
|
2022-10-21 03:03:05 +04:00
|
|
|
entry, err = guestbook.NewEntry(name, r.FormValue("message"),
|
|
|
|
r.FormValue("website"), len(r.FormValue("hide_website")) != 0)
|
2022-10-19 03:25:43 +04:00
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if r.Header.Get("Content-Type") == "application/json" {
|
|
|
|
cid := struct {
|
|
|
|
CaptchaID string `json:"captcha_id"`
|
|
|
|
}{}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&cid); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
solved, err := justcaptcha.CheckCaptcha(cid.CaptchaID, h.captchaAddr)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
log.Println("justcaptcha:", err)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !solved {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(entry); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.db.NewEntry(entry)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, entry.Message, http.StatusInternalServerError)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookHandlers) Reply(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var reply *guestbook.Reply
|
2022-10-21 03:03:05 +04:00
|
|
|
|
2022-10-19 03:25:43 +04:00
|
|
|
if r.Header.Get("X-Password") != h.password {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
return
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
|
|
|
|
r.ParseForm()
|
|
|
|
|
|
|
|
reply, err = guestbook.NewReply(id, r.FormValue("reply"))
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
|
|
|
return
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
} else if r.Header.Get("Content-Type") == "application/json" {
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(reply); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := h.db.NewReply(reply); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
2022-10-21 03:03:05 +04:00
|
|
|
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *GuestbookHandlers) Delete(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("X-Password") != h.password {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
entryID, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64)
|
|
|
|
if err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
2022-10-19 03:25:43 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasSuffix(r.URL.Path, "reply") {
|
|
|
|
if err := h.db.DeleteReply(entryID); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := h.db.DeleteEntry(entryID); err != nil {
|
2022-10-21 03:03:05 +04:00
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
2022-10-19 03:25:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|