1
0

Made use of http.Error(). Removed error return from New() func. Added StatusCreated header to Reply() method.

This commit is contained in:
Alexander Andreev 2022-10-21 03:03:05 +04:00
parent 059efc4004
commit 6aa2bd10ff
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -2,10 +2,10 @@ package handlers
import ( import (
"encoding/json" "encoding/json"
"fmt"
"justguestbook/internal/guestbook" "justguestbook/internal/guestbook"
"justguestbook/pkg/justcaptcha"
"justguestbook/pkg/server" "justguestbook/pkg/server"
"justguestbook/pkg/server/justcaptcha" "log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -20,14 +20,14 @@ type GuestbookHandlers struct {
captchaAddr string captchaAddr string
} }
func New(owner, password, anonymousName string, defaultPageSize int64, guestbook guestbook.Guestbook, captchaAddr string) (*GuestbookHandlers, error) { func New(owner, password, anonymousName string, defaultPageSize int64, guestbook guestbook.Guestbook, captchaAddr string) *GuestbookHandlers {
return &GuestbookHandlers{ return &GuestbookHandlers{
owner: owner, owner: owner,
password: password, password: password,
anonymousName: anonymousName, anonymousName: anonymousName,
defaultPageSize: defaultPageSize, defaultPageSize: defaultPageSize,
db: guestbook, db: guestbook,
captchaAddr: captchaAddr}, nil captchaAddr: captchaAddr}
} }
func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) { func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
@ -36,14 +36,14 @@ func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
var page_size int64 = h.defaultPageSize var page_size int64 = h.defaultPageSize
if r.URL.Query().Get("p") != "" { if r.URL.Query().Get("p") != "" {
page_num, err = strconv.ParseInt(r.URL.Query().Get("p"), 10, 32) page_num, err = strconv.ParseInt(r.URL.Query().Get("p"), 10, 64)
if err != nil { if err != nil {
page_num = 1 page_num = 1
} }
} }
if r.URL.Query().Get("ps") != "" { if r.URL.Query().Get("ps") != "" {
page_size, err = strconv.ParseInt(r.URL.Query().Get("ps"), 10, 32) page_size, err = strconv.ParseInt(r.URL.Query().Get("ps"), 10, 64)
if err != nil { if err != nil {
page_size = h.defaultPageSize page_size = h.defaultPageSize
} }
@ -51,13 +51,7 @@ func (h *GuestbookHandlers) Entries(w http.ResponseWriter, r *http.Request) {
entries, err := h.db.Entries(page_num, page_size) entries, err := h.db.Entries(page_num, page_size)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err)
}
if len(entries) == 0 {
w.WriteHeader(http.StatusNotFound)
return
} }
guestbookEntries := struct { guestbookEntries := struct {
@ -85,8 +79,8 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
solved, err := justcaptcha.CheckCaptcha(r.FormValue("captcha_id"), h.captchaAddr) solved, err := justcaptcha.CheckCaptcha(r.FormValue("captcha_id"), h.captchaAddr)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err) log.Println("justcaptcha:", err)
return return
} }
@ -100,11 +94,10 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
name = h.anonymousName name = h.anonymousName
} }
entry, err = guestbook.NewEntry(name, entry, err = guestbook.NewEntry(name, r.FormValue("message"),
r.FormValue("website"), r.FormValue("message"), len(r.FormValue("hide_website")) != 0) r.FormValue("website"), len(r.FormValue("hide_website")) != 0)
if err != nil { if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err)
return return
} }
} else if r.Header.Get("Content-Type") == "application/json" { } else if r.Header.Get("Content-Type") == "application/json" {
@ -112,15 +105,14 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
CaptchaID string `json:"captcha_id"` CaptchaID string `json:"captcha_id"`
}{} }{}
if err := json.NewDecoder(r.Body).Decode(&cid); err != nil { if err := json.NewDecoder(r.Body).Decode(&cid); err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err)
return return
} }
solved, err := justcaptcha.CheckCaptcha(cid.CaptchaID, h.captchaAddr) solved, err := justcaptcha.CheckCaptcha(cid.CaptchaID, h.captchaAddr)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err) log.Println("justcaptcha:", err)
return return
} }
@ -130,16 +122,14 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
} }
if err := json.NewDecoder(r.Body).Decode(entry); err != nil { if err := json.NewDecoder(r.Body).Decode(entry); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err)
return return
} }
} }
err = h.db.NewEntry(entry) err = h.db.NewEntry(entry)
if err != nil { if err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, entry.Message, http.StatusInternalServerError)
fmt.Fprint(w, entry.Message)
return return
} }
@ -148,6 +138,7 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
func (h *GuestbookHandlers) Reply(w http.ResponseWriter, r *http.Request) { func (h *GuestbookHandlers) Reply(w http.ResponseWriter, r *http.Request) {
var reply *guestbook.Reply var reply *guestbook.Reply
if r.Header.Get("X-Password") != h.password { if r.Header.Get("X-Password") != h.password {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
return return
@ -155,8 +146,8 @@ func (h *GuestbookHandlers) Reply(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64) id, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64)
if err != nil { if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err) return
} }
if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" { if r.Header.Get("Content-Type") == "application/x-www-form-urlencoded" {
@ -164,21 +155,22 @@ func (h *GuestbookHandlers) Reply(w http.ResponseWriter, r *http.Request) {
reply, err = guestbook.NewReply(id, r.FormValue("reply")) reply, err = guestbook.NewReply(id, r.FormValue("reply"))
if err != nil { if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err) return
} }
} else if r.Header.Get("Content-Type") == "application/json" { } else if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(reply); err != nil { if err := json.NewDecoder(r.Body).Decode(reply); err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err)
return return
} }
} }
if err := h.db.NewReply(reply); err != nil { if err := h.db.NewReply(reply); err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err) return
} }
w.WriteHeader(http.StatusCreated)
} }
func (h *GuestbookHandlers) Delete(w http.ResponseWriter, r *http.Request) { func (h *GuestbookHandlers) Delete(w http.ResponseWriter, r *http.Request) {
@ -189,20 +181,17 @@ func (h *GuestbookHandlers) Delete(w http.ResponseWriter, r *http.Request) {
entryID, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64) entryID, err := strconv.ParseInt(server.GetURLParam(r, "entry"), 10, 64)
if err != nil { if err != nil {
w.WriteHeader(http.StatusUnprocessableEntity) http.Error(w, err.Error(), http.StatusUnprocessableEntity)
fmt.Fprint(w, err)
return return
} }
if strings.HasSuffix(r.URL.Path, "reply") { if strings.HasSuffix(r.URL.Path, "reply") {
if err := h.db.DeleteReply(entryID); err != nil { if err := h.db.DeleteReply(entryID); err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err)
} }
} else { } else {
if err := h.db.DeleteEntry(entryID); err != nil { if err := h.db.DeleteEntry(entryID); err != nil {
w.WriteHeader(http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Fprint(w, err)
} }
} }
} }