73 lines
1.9 KiB
Go
73 lines
1.9 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"html/template"
|
||
|
"log"
|
||
|
"net/http"
|
||
|
"time"
|
||
|
|
||
|
"github.com/gorilla/feeds"
|
||
|
"streifling.com/jason/cpolis/cmd/data"
|
||
|
)
|
||
|
|
||
|
func WriteArticle() http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func FinishArticle(l *data.ArticleList) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
article := data.Article{}
|
||
|
var err error
|
||
|
|
||
|
article.Title, err = data.ConvertToPlain(r.PostFormValue("editor-title"))
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
article.Desc, err = data.ConvertToPlain(r.PostFormValue("editor-desc"))
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
article.Content, err = data.ConvertToHTML(r.PostFormValue("editor-text"))
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
article.Created = time.Now()
|
||
|
l.Add(article)
|
||
|
|
||
|
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func PublishArticle(f *data.Feed, l *data.ArticleList, i int) http.HandlerFunc {
|
||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||
|
article, ok := l.Release(i)
|
||
|
if !ok {
|
||
|
// TODO: Warnung anzeigen
|
||
|
// data.Msg = "Alle Felder müssen ausgefüllt werden."
|
||
|
// template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", data)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
f.Add(&feeds.Item{
|
||
|
Title: article.Title,
|
||
|
Created: article.Created,
|
||
|
Description: article.Desc,
|
||
|
Content: article.Content,
|
||
|
})
|
||
|
f.Save("rss.gob")
|
||
|
|
||
|
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
|
||
|
}
|
||
|
}
|