package ui import ( "html/template" "log" "net/http" "time" "github.com/google/uuid" "github.com/gorilla/feeds" "streifling.com/jason/cpolis/cmd/data" ) func ShowHub() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles("web/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func WriteArticle() http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles("web/templates/editor.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func FinishArticle(l *data.ArticleList) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { article := new(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.UUID = uuid.New() article.Created = time.Now() l.Add(article) tmpl, err := template.ParseFiles("web/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", l.Get()) } } func ReviewArticle(l *data.ArticleList) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { uuid, err := uuid.Parse(r.PostFormValue("uuid")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } for _, article := range l.Get() { if article.UUID == uuid { tmpl, err := template.ParseFiles("web/templates/to-be-published.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article) return } } tmpl, err := template.ParseFiles("web/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } } func PublishArticle(f *data.Feed, l *data.ArticleList) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { uuid, err := uuid.Parse(r.PostFormValue("uuid")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } article, ok := l.Release(uuid) if !ok { // TODO: Warnung anzeigen // msg = "Alle Felder müssen ausgefüllt werden." // tmpl, err := template.ParseFiles("web/templates/add-user.html") // template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg) return } f.Add(&feeds.Item{ Title: article.Title, Created: article.Created, Description: article.Desc, Content: article.Content, }) f.Save("tmp/rss.gob") tmpl, err := template.ParseFiles("web/templates/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil) } }