From 714d4fdb73339f6813d185215b9aa71e77404f1c Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Tue, 23 May 2023 00:20:01 +0400 Subject: [PATCH] Removed DeleteUnusedCategories(). Impl. NewCategory(). --- internal/http/api_handlers.go | 62 +++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/internal/http/api_handlers.go b/internal/http/api_handlers.go index 7f658ee..1d49bc6 100644 --- a/internal/http/api_handlers.go +++ b/internal/http/api_handlers.go @@ -3,7 +3,6 @@ package http import ( "fmt" "image/jpeg" - "log" "net/http" "strconv" "strings" @@ -250,10 +249,6 @@ func (h *MindflowApiHandlers) New(w http.ResponseWriter, r *http.Request) { return } - if err := h.db.DeleteUnusedCategories(); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } - http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently) } @@ -297,10 +292,6 @@ func (h *MindflowApiHandlers) Edit(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - - if err := h.db.DeleteUnusedCategories(); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - } } func (h *MindflowApiHandlers) Delete(w http.ResponseWriter, r *http.Request) { @@ -309,8 +300,51 @@ func (h *MindflowApiHandlers) Delete(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } +} - if err := h.db.DeleteUnusedCategories(); err != nil { +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.ID, err = h.db.NewCategory(r.FormValue("name")) + if 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) 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") != "" { + category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64) + + if category.ID == 0 { + category.ID, err = h.db.NewCategory(r.FormValue("new-category")) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + } + } else { + http.Error(w, "wrong category id passed", http.StatusBadRequest) + return + } + + category.Name = r.FormValue("name") + } + + if err = h.db.EditCategory(category); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } @@ -322,11 +356,3 @@ func (h *MindflowApiHandlers) DeleteCategory(w http.ResponseWriter, r *http.Requ return } } - -func (h *MindflowApiHandlers) DeleteUnusedCategories(w http.ResponseWriter, r *http.Request) { - if err := h.db.DeleteUnusedCategories(); err != nil { - log.Println("Cannot delete unused categories:", err) - http.Error(w, "Cannot delete unused categories: "+err.Error(), http.StatusInternalServerError) - return - } -}