diff --git a/internal/http/api_handlers.go b/internal/http/api_handlers.go index aa4b216..9cafcc6 100644 --- a/internal/http/api_handlers.go +++ b/internal/http/api_handlers.go @@ -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) {