1
0

New handler for Mindflow.

This commit is contained in:
Alexander Andreev 2023-05-14 03:36:39 +04:00
parent 0903f2c2c6
commit 8cbd8f143f
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -8,6 +8,8 @@ import (
"strings"
"time"
"git.arav.su/Arav/dwelling-home/pkg/mindflow"
"git.arav.su/Arav/dwelling-home/pkg/mindflow/database/sqlite"
"git.arav.su/Arav/justcaptcha/pkg/captcha"
"git.arav.su/Arav/justcaptcha/pkg/captcha/inmemdb"
"git.arav.su/Arav/justcaptcha/pkg/dwcaptcha"
@ -211,13 +213,40 @@ func (h *CaptchaApiHandlers) Image(w http.ResponseWriter, r *http.Request) {
// Mindflow API ////////////////////////////////////////////////////////////////
type MindflowApiHandlers struct{}
type MindflowApiHandlers struct {
db *sqlite.SQLiteMindflow
}
func NewMindflowApiHandlers() *MindflowApiHandlers {
return &MindflowApiHandlers{}
func NewMindflowApiHandlers(db *sqlite.SQLiteMindflow) *MindflowApiHandlers {
return &MindflowApiHandlers{db: db}
}
func (h *MindflowApiHandlers) New(w http.ResponseWriter, r *http.Request) {
var post *mindflow.Post
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 {
// TODO: create category
}
post, err = mindflow.NewPost(mindflow.Category{ID: category_id}, r.FormValue("title"), r.FormValue("body"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if err = h.db.New(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) Edit(w http.ResponseWriter, r *http.Request) {