cpolis/cmd/ui/articles.go

114 lines
3.1 KiB
Go

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) {
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}
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.UUID = uuid.New()
article.Created = time.Now()
l.Add(article)
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}
func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/unpublished-articles.html")).ExecuteTemplate(w, "page-content", l.List)
}
}
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.List {
if article.UUID == uuid {
template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article)
return
}
}
template.Must(template.ParseFiles("web/templates/hub.html")).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."
// template.Must(template.ParseFiles("web/templates/add-user.html")).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")
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil)
}
}