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