cpolis/cmd/ui/articles.go

154 lines
4.6 KiB
Go
Raw Normal View History

package ui
import (
"html/template"
"log"
"net/http"
"time"
2024-03-01 11:30:31 +01:00
"github.com/google/uuid"
"github.com/gorilla/feeds"
"streifling.com/jason/cpolis/cmd/data"
)
2024-03-03 09:16:49 +01:00
func ShowHub(s *data.CookieStore) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"])
2024-03-01 11:30:31 +01:00
}
}
2024-03-03 09:16:49 +01:00
func WriteArticle(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("web/templates/editor.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
}
2024-03-03 09:16:49 +01:00
func FinishArticle(l *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2024-03-01 21:01:38 +01:00
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
}
2024-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-01 11:30:31 +01:00
article.UUID = uuid.New()
2024-03-03 09:16:49 +01:00
article.Author = session.Values["name"].(string)
article.Created = time.Now()
2024-03-03 09:16:49 +01:00
article.AuthorID = session.Values["id"].(int64)
2024-03-01 11:30:31 +01:00
l.Add(article)
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
}
}
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
}
}
2024-03-03 09:16:49 +01:00
func ReviewArticle(l *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
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() {
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-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
2024-03-01 11:30:31 +01:00
}
}
2024-03-03 09:16:49 +01:00
func PublishArticle(f *data.Feed, l *data.ArticleList, s *data.CookieStore) http.HandlerFunc {
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)
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)
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-03-03 09:16:49 +01:00
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/hub.html")
2024-03-03 09:16:49 +01:00
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"])
}
}