Fixed mindflow API handlers. Now they are working.
This commit is contained in:
parent
86c1d1e00c
commit
aac097feb8
@ -222,22 +222,30 @@ func NewMindflowApiHandlers(db mindflow.Mindflow) *MindflowApiHandlers {
|
|||||||
|
|
||||||
func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) {
|
func (h *MindflowApiHandlers) NewPost(w http.ResponseWriter, r *http.Request) {
|
||||||
var post *mindflow.Post
|
var post *mindflow.Post
|
||||||
|
var category *mindflow.Category
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
category_id, _ := strconv.ParseInt(r.FormValue("category"), 10, 64)
|
if r.FormValue("category") == "0" {
|
||||||
|
category, err = mindflow.NewCategory(r.FormValue("new-category"))
|
||||||
if category_id == 0 {
|
|
||||||
category_id, err = h.db.NewCategory(r.FormValue("new-category"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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) {
|
func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) {
|
||||||
var post *mindflow.Post
|
var post *mindflow.Post
|
||||||
|
var category *mindflow.Category
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
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, "\n\r", "\n")
|
||||||
body = strings.ReplaceAll(body, "\r", "\n")
|
body = strings.ReplaceAll(body, "\r", "\n")
|
||||||
|
|
||||||
var category mindflow.Category
|
|
||||||
|
|
||||||
if r.FormValue("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)
|
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
||||||
|
|
||||||
if category.ID == 0 {
|
if category.ID == 0 {
|
||||||
category.ID, err = h.db.NewCategory(r.FormValue("new-category"))
|
category.ID, err = h.db.NewCategory(category)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
category = &mindflow.Category{}
|
||||||
category.ID, _ = strconv.ParseInt(r.FormValue("old-category"), 10, 64)
|
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 {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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") {
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
category.ID, err = h.db.NewCategory(r.FormValue("name"))
|
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(err.Error(), "Title: "+r.FormValue("title")+" | Body: "+r.FormValue("body"), w)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
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) {
|
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") {
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
|
|
||||||
if r.FormValue("category") != "" {
|
if r.FormValue("category") != "" || r.FormValue("category") != "0" {
|
||||||
category.ID, _ = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
category, err = mindflow.NewCategory(r.FormValue("name"))
|
||||||
|
|
||||||
if category.ID == 0 {
|
|
||||||
category.ID, err = h.db.NewCategory(r.FormValue("new-category"))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
category.ID, _ = strconv.ParseInt(GetURLParam(r, "id"), 10, 64)
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "wrong category id passed", http.StatusBadRequest)
|
http.Error(w, "empty category id passed", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
category.Name = r.FormValue("name")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = h.db.EditCategory(category); err != nil {
|
if err = h.db.EditCategory(category); err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user