cpolis/cmd/ui/articles.go

172 lines
5.0 KiB
Go
Raw Normal View History

package ui
import (
"html/template"
"log"
"net/http"
2024-03-06 20:53:17 +01:00
"strconv"
"time"
"git.streifling.com/jason/rss"
"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-06 20:53:17 +01:00
func WriteArticle(db *data.DB) http.HandlerFunc {
2024-03-03 13:56:49 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-06 20:53:17 +01:00
tags, err := db.GetTagList()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 13:56:49 +01:00
tmpl, err := template.ParseFiles("web/templates/editor.html")
2024-03-06 20:53:17 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags)
2024-03-03 13:56:49 +01:00
}
}
2024-03-06 20:53:17 +01:00
func FinishArticle(db *data.DB, 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-05 18:20:34 +01:00
r.ParseForm()
article.Tags = append(article.Tags, r.Form["tags"]...)
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)
}
article.AuthorID = session.Values["id"].(int64)
2024-03-06 20:53:17 +01:00
db.AddArticle(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-06 20:53:17 +01:00
func ShowUnpublishedArticles(db *data.DB) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-06 20:53:17 +01:00
articles, err := db.GetUnpublishedArticles()
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-02 09:09:55 +01:00
tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html")
2024-03-06 20:53:17 +01:00
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles)
2024-03-01 11:30:31 +01:00
}
}
2024-03-06 20:53:17 +01:00
func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc {
2024-03-01 11:30:31 +01:00
return func(w http.ResponseWriter, r *http.Request) {
2024-03-06 20:53:17 +01:00
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
2024-03-01 11:30:31 +01:00
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-06 20:53:17 +01:00
article, err := db.GetArticle(id)
if err != nil {
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article)
return
2024-03-01 11:30:31 +01:00
}
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-06 20:53:17 +01:00
func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
2024-03-06 20:53:17 +01:00
id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64)
2024-03-01 11:30:31 +01:00
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-06 20:53:17 +01:00
if err = db.UpdateArticle(id, "published", true); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
article, err := db.GetArticle(id)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2024-03-03 13:56: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)
}
c.Add(&rss.Item{
Title: article.Title,
Author: session.Values["name"].(string),
PubDate: article.Created.Format(time.RFC1123Z),
Description: article.Desc,
Content: &rss.Content{Value: article.Content},
2024-03-05 18:20:34 +01:00
Categories: article.Tags,
})
c.Save("tmp/rss.gob")
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"])
}
}