Let's reduce a mess by splitting APIs by distinct files.
This commit is contained in:
parent
d18374e72a
commit
60e71378ba
59
internal/http/api_captcha_handlers.go
Normal file
59
internal/http/api_captcha_handlers.go
Normal file
@ -0,0 +1,59 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
|
||||
)
|
||||
|
||||
type CaptchaApiHandlers struct {
|
||||
Expiry time.Duration
|
||||
}
|
||||
|
||||
func NewCaptchaApiHandlers(expiry time.Duration) *CaptchaApiHandlers {
|
||||
inmemdb.SetExpiry(expiry)
|
||||
return &CaptchaApiHandlers{Expiry: expiry}
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) New(w http.ResponseWriter, r *http.Request) {
|
||||
dwc := dwcaptcha.NewDwellingCaptcha(h.Expiry)
|
||||
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
fmt.Fprint(w, id)
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
||||
captchaID := captcha.ID(GetURLParam(r, "id"))
|
||||
|
||||
if r.URL.Query().Has("remove") {
|
||||
inmemdb.Remove(captchaID)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if solved := inmemdb.IsSolved(captchaID); !solved {
|
||||
http.Error(w, "wrong answer", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
||||
id := captcha.ID(GetURLParam(r, "id"))
|
||||
|
||||
image := inmemdb.Image(id, r.URL.Query().Get("style"))
|
||||
if image == nil {
|
||||
http.Error(w, "image not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Disposition", "inline; filename=\""+string(id)+"\"")
|
||||
|
||||
jpeg.Encode(w, *image, &jpeg.Options{Quality: 20})
|
||||
}
|
155
internal/http/api_guestbook_handlers.go
Normal file
155
internal/http/api_guestbook_handlers.go
Normal file
@ -0,0 +1,155 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
|
||||
"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"))) {
|
||||
ForbiddenError("Wrong answer given.", "Here's your message:"+r.FormValue("message"), w)
|
||||
return
|
||||
}
|
||||
|
||||
if r.FormValue("name") == "" {
|
||||
r.Form.Set("name", "Anonymous")
|
||||
}
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
|
||||
r.FormValue("website"), r.FormValue("hide_website") != "")
|
||||
if err != nil {
|
||||
InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.NewEntry(entry); err != nil {
|
||||
InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
||||
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")
|
||||
}
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
entry.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
}
|
||||
|
||||
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) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
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()
|
||||
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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()
|
||||
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeleteReply(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
@ -1,371 +0,0 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
|
||||
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
|
||||
"git.arav.su/Arav/justguestbook"
|
||||
)
|
||||
|
||||
// Guestbook API ///////////////////////////////////////////////////////////////
|
||||
|
||||
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"))) {
|
||||
ForbiddenError("Wrong answer given.", "Here's your message:"+r.FormValue("message"), w)
|
||||
return
|
||||
}
|
||||
|
||||
if r.FormValue("name") == "" {
|
||||
r.Form.Set("name", "Anonymous")
|
||||
}
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
entry, err = justguestbook.NewEntry(r.FormValue("name"), message,
|
||||
r.FormValue("website"), r.FormValue("hide_website") != "")
|
||||
if err != nil {
|
||||
InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.NewEntry(entry); err != nil {
|
||||
InternalError(err.Error(), "Here's your message:"+r.FormValue("message"), w)
|
||||
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")
|
||||
}
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
entry.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
}
|
||||
|
||||
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) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
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()
|
||||
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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()
|
||||
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
|
||||
message := strings.ReplaceAll(r.FormValue("message"), "\r\n", "\n")
|
||||
message = strings.ReplaceAll(message, "\n\r", "\n")
|
||||
message = strings.ReplaceAll(message, "\r", "\n")
|
||||
|
||||
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) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeleteReply(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// CAPTCHA API /////////////////////////////////////////////////////////////////
|
||||
|
||||
type CaptchaApiHandlers struct {
|
||||
Expiry time.Duration
|
||||
}
|
||||
|
||||
func NewCaptchaApiHandlers(expiry time.Duration) *CaptchaApiHandlers {
|
||||
inmemdb.SetExpiry(expiry)
|
||||
return &CaptchaApiHandlers{Expiry: expiry}
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) New(w http.ResponseWriter, r *http.Request) {
|
||||
dwc := dwcaptcha.NewDwellingCaptcha(h.Expiry)
|
||||
_, id := inmemdb.New(r.RemoteAddr, dwc)
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
fmt.Fprint(w, id)
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) Solve(w http.ResponseWriter, r *http.Request) {
|
||||
captchaID := captcha.ID(GetURLParam(r, "id"))
|
||||
|
||||
if r.URL.Query().Has("remove") {
|
||||
inmemdb.Remove(captchaID)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
|
||||
if solved := inmemdb.IsSolved(captchaID); !solved {
|
||||
http.Error(w, "wrong answer", http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *CaptchaApiHandlers) Image(w http.ResponseWriter, r *http.Request) {
|
||||
id := captcha.ID(GetURLParam(r, "id"))
|
||||
|
||||
image := inmemdb.Image(id, r.URL.Query().Get("style"))
|
||||
if image == nil {
|
||||
http.Error(w, "image not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Disposition", "inline; filename=\""+string(id)+"\"")
|
||||
|
||||
jpeg.Encode(w, *image, &jpeg.Options{Quality: 20})
|
||||
}
|
||||
|
||||
// Mindflow API ////////////////////////////////////////////////////////////////
|
||||
|
||||
type MindflowApiHandlers struct {
|
||||
db mindflow.Mindflow
|
||||
}
|
||||
|
||||
func NewMindflowApiHandlers(db mindflow.Mindflow) *MindflowApiHandlers {
|
||||
return &MindflowApiHandlers{db: db}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) {
|
||||
var post *mindflow.Post
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
if r.FormValue("category") == "0" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("new-category"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
category.ID, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
category = &mindflow.Category{}
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
||||
}
|
||||
|
||||
post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("body"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.NewPost(post); err != nil {
|
||||
InternalError(err.Error(), "Title: "+r.FormValue("title")+" | Body: "+r.FormValue("body"), w)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently)
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) {
|
||||
var post *mindflow.Post
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
body := strings.ReplaceAll(r.FormValue("body"), "\r\n", "\n")
|
||||
body = strings.ReplaceAll(body, "\n\r", "\n")
|
||||
body = strings.ReplaceAll(body, "\r", "\n")
|
||||
|
||||
if r.FormValue("category") != "" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("new-category"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
||||
|
||||
if category.ID == 0 {
|
||||
category.ID, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
category = &mindflow.Category{}
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("old-category"), 10, 64)
|
||||
}
|
||||
|
||||
post, err = mindflow.NewPost(*category, r.FormValue("title"), body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
post.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
}
|
||||
|
||||
if err = h.db.EditPost(post); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) DeletePost(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeletePost(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) NewCategory(w http.ResponseWriter, r *http.Request) {
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) EditCategory(w http.ResponseWriter, r *http.Request) {
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
if r.FormValue("category") != "" || r.FormValue("category") != "0" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
category.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
} else {
|
||||
http.Error(w, "empty category id passed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.EditCategory(category); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) DeleteCategory(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeleteCategory(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
167
internal/http/api_mindflow_handlers.go
Normal file
167
internal/http/api_mindflow_handlers.go
Normal file
@ -0,0 +1,167 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
|
||||
)
|
||||
|
||||
type MindflowApiHandlers struct {
|
||||
db mindflow.Mindflow
|
||||
}
|
||||
|
||||
func NewMindflowApiHandlers(db mindflow.Mindflow) *MindflowApiHandlers {
|
||||
return &MindflowApiHandlers{db: db}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) {
|
||||
var post *mindflow.Post
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
if r.FormValue("category") == "0" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("new-category"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
category.ID, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
category = &mindflow.Category{}
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
||||
}
|
||||
|
||||
post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("body"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.NewPost(post); err != nil {
|
||||
InternalError(err.Error(), "Title: "+r.FormValue("title")+" | Body: "+r.FormValue("body"), w)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently)
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) {
|
||||
var post *mindflow.Post
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
body := strings.ReplaceAll(r.FormValue("body"), "\r\n", "\n")
|
||||
body = strings.ReplaceAll(body, "\n\r", "\n")
|
||||
body = strings.ReplaceAll(body, "\r", "\n")
|
||||
|
||||
if r.FormValue("category") != "" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("new-category"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
||||
|
||||
if category.ID == 0 {
|
||||
category.ID, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
category = &mindflow.Category{}
|
||||
category.ID, _ = strconv.ParseInt(r.FormValue("old-category"), 10, 64)
|
||||
}
|
||||
|
||||
post, err = mindflow.NewPost(*category, r.FormValue("title"), body)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
post.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
}
|
||||
|
||||
if err = h.db.EditPost(post); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) DeletePost(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeletePost(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) NewCategory(w http.ResponseWriter, r *http.Request) {
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
_, err = h.db.NewCategory(category)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) EditCategory(w http.ResponseWriter, r *http.Request) {
|
||||
var category *mindflow.Category
|
||||
var err error
|
||||
|
||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||
r.ParseForm()
|
||||
|
||||
if r.FormValue("category") != "" || r.FormValue("category") != "0" {
|
||||
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
category.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
} else {
|
||||
http.Error(w, "empty category id passed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err = h.db.EditCategory(category); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *MindflowApiHandlers) DeleteCategory(w http.ResponseWriter, r *http.Request) {
|
||||
id, _ := strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||
if err := h.db.DeleteCategory(id); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user