diff --git a/internal/http/api_handlers.go b/internal/http/api_handlers.go index e5a26db..f3d497a 100644 --- a/internal/http/api_handlers.go +++ b/internal/http/api_handlers.go @@ -222,22 +222,30 @@ func NewMindflowApiHandlers(db mindflow.Mindflow) *MindflowApiHandlers { 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() - category_id, _ := strconv.ParseInt(r.FormValue("category"), 10, 64) - - if category_id == 0 { - category_id, err = h.db.NewCategory(r.FormValue("new-category")) + 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(mindflow.Category{ID: category_id}, r.FormValue("title"), r.FormValue("body")) + post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("body")) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -254,6 +262,7 @@ func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) { 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") { @@ -263,23 +272,28 @@ func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) { body = strings.ReplaceAll(body, "\n\r", "\n") body = strings.ReplaceAll(body, "\r", "\n") - var category mindflow.Category - 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(r.FormValue("new-category")) + 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) + post, err = mindflow.NewPost(*category, r.FormValue("title"), body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -309,14 +323,18 @@ func (h *MindflowApiHandlers) NewCategory(w http.ResponseWriter, r *http.Request if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") { r.ParseForm() - category.ID, err = h.db.NewCategory(r.FormValue("name")) + category, err = mindflow.NewCategory(r.FormValue("name")) if err != nil { - InternalError(err.Error(), "Title: "+r.FormValue("title")+" | Body: "+r.FormValue("body"), w) + http.Error(w, err.Error(), http.StatusInternalServerError) return } } - http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently) + _, 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) { @@ -326,22 +344,17 @@ func (h *MindflowApiHandlers) EditCategory(w http.ResponseWriter, r *http.Reques 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 - } + 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, "wrong category id passed", http.StatusBadRequest) + http.Error(w, "empty category id passed", http.StatusBadRequest) return } - - category.Name = r.FormValue("name") } if err = h.db.EditCategory(category); err != nil {