2023-05-23 04:36:04 +04:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
|
2023-05-27 20:56:32 +04:00
|
|
|
"git.arav.su/Arav/httpr"
|
2023-05-23 04:36:04 +04:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-09-25 00:23:06 +04:00
|
|
|
post, err = mindflow.NewPost(*category, r.FormValue("title"), r.FormValue("url"), r.FormValue("body"))
|
2023-05-23 04:36:04 +04:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.db.NewPost(post); err != nil {
|
2023-05-27 20:47:53 +04:00
|
|
|
msg := strings.Join([]string{"Title:", r.FormValue("title"), "| Body:", r.FormValue("body")}, " ")
|
2023-08-12 23:51:26 +04:00
|
|
|
Error(w, http.StatusInternalServerError, err.Error(), msg, r.Referer())
|
2023-05-23 04:36:04 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, "/mindflow/admin", http.StatusMovedPermanently)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MindflowApiHandlers) EditPost(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var post *mindflow.Post
|
2023-07-23 23:12:14 +04:00
|
|
|
var category mindflow.Category
|
2023-05-23 04:36:04 +04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if strings.Contains(r.Header.Get("Content-Type"), "application/x-www-form-urlencoded") {
|
|
|
|
r.ParseForm()
|
|
|
|
|
2023-05-28 04:52:09 +04:00
|
|
|
body := cleanupNewlines(r.FormValue("body"))
|
2023-09-25 00:23:06 +04:00
|
|
|
url := r.FormValue("url")
|
2023-05-23 04:36:04 +04:00
|
|
|
|
2023-07-23 23:12:14 +04:00
|
|
|
category.ID, err = strconv.ParseInt(r.FormValue("category"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
2023-05-23 04:36:04 +04:00
|
|
|
}
|
|
|
|
|
2023-09-25 00:23:06 +04:00
|
|
|
post, err = mindflow.NewPost(category, r.FormValue("title"), url, body)
|
2023-05-23 04:36:04 +04:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-27 20:56:32 +04:00
|
|
|
post.ID, _ = strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
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
|
|
|
|
}
|
2023-05-27 20:56:32 +04:00
|
|
|
category.ID, _ = strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
} 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) {
|
2023-05-27 20:56:32 +04:00
|
|
|
id, _ := strconv.ParseInt(httpr.Param(r, "id"), 10, 64)
|
2023-05-23 04:36:04 +04:00
|
|
|
if err := h.db.DeleteCategory(id); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|