Made use of http.Error(). Removed error return from New() func. Added StatusCreated header to Reply() method.
This commit is contained in:
parent
059efc4004
commit
6aa2bd10ff
@ -2,10 +2,10 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"justguestbook/internal/guestbook"
|
||||
"justguestbook/pkg/justcaptcha"
|
||||
"justguestbook/pkg/server"
|
||||
"justguestbook/pkg/server/justcaptcha"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -20,14 +20,14 @@ type GuestbookHandlers struct {
|
||||
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{
|
||||
owner: owner,
|
||||
password: password,
|
||||
anonymousName: anonymousName,
|
||||
defaultPageSize: defaultPageSize,
|
||||
db: guestbook,
|
||||
captchaAddr: captchaAddr}, nil
|
||||
captchaAddr: captchaAddr}
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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 {
|
||||
page_num = 1
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
}
|
||||
|
||||
if len(entries) == 0 {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("justcaptcha:", err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -100,11 +94,10 @@ func (h *GuestbookHandlers) New(w http.ResponseWriter, r *http.Request) {
|
||||
name = h.anonymousName
|
||||
}
|
||||
|
||||
entry, err = guestbook.NewEntry(name,
|
||||
r.FormValue("website"), r.FormValue("message"), len(r.FormValue("hide_website")) != 0)
|
||||
entry, err = guestbook.NewEntry(name, r.FormValue("message"),
|
||||
r.FormValue("website"), len(r.FormValue("hide_website")) != 0)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
} 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"`
|
||||
}{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&cid); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
solved, err := justcaptcha.CheckCaptcha(cid.CaptchaID, h.captchaAddr)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
log.Println("justcaptcha:", err)
|
||||
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 {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
err = h.db.NewEntry(entry)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, entry.Message)
|
||||
http.Error(w, entry.Message, http.StatusInternalServerError)
|
||||
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) {
|
||||
var reply *guestbook.Reply
|
||||
|
||||
if r.Header.Get("X-Password") != h.password {
|
||||
w.WriteHeader(http.StatusForbidden)
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
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"))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
} else if r.Header.Get("Content-Type") == "application/json" {
|
||||
if err := json.NewDecoder(r.Body).Decode(reply); err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := h.db.NewReply(reply); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnprocessableEntity)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusUnprocessableEntity)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "reply") {
|
||||
if err := h.db.DeleteReply(entryID); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
} else {
|
||||
if err := h.db.DeleteEntry(entryID); err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprint(w, err)
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user