2024-03-09 10:25:20 +01:00
|
|
|
package view
|
2024-02-27 14:10:27 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-03-06 20:53:17 +01:00
|
|
|
"strconv"
|
2024-02-27 14:10:27 +01:00
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
"streifling.com/jason/cpolis/cmd/control"
|
|
|
|
"streifling.com/jason/cpolis/cmd/model"
|
2024-02-27 14:10:27 +01:00
|
|
|
)
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
func ShowHub(s *control.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-09 10:25:20 +01:00
|
|
|
func WriteArticle(db *model.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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
func FinishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-09 10:25:20 +01:00
|
|
|
article := new(model.Article)
|
2024-02-27 14:10:27 +01:00
|
|
|
var err error
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
article.Title, err = control.ConvertToPlain(r.PostFormValue("editor-title"))
|
2024-02-27 14:10:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
article.Desc, err = control.ConvertToPlain(r.PostFormValue("editor-desc"))
|
2024-02-27 14:10:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
article.Content, err = control.ConvertToHTML(r.PostFormValue("editor-text"))
|
2024-02-27 14:10:27 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
article.AuthorID = session.Values["id"].(int64)
|
2024-03-07 15:31:00 +01:00
|
|
|
article.ID, err = db.AddArticle(article)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.ParseForm()
|
|
|
|
tags := make([]int64, 0)
|
|
|
|
for _, tag := range r.Form["tags"] {
|
|
|
|
tagID, err := strconv.ParseInt(tag, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tags = append(tags, tagID)
|
|
|
|
}
|
|
|
|
if err = db.WriteArticleTags(article.ID, tags); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-02-27 14:10:27 +01:00
|
|
|
|
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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc {
|
2024-03-01 11:30:31 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-03-09 10:12:46 +01:00
|
|
|
articles, err := db.GetCertainArticles(false)
|
2024-03-06 20:53:17 +01:00
|
|
|
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-09 10:25:20 +01:00
|
|
|
func ReviewArticle(db *model.DB, s *control.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
|
|
|
|
2024-03-07 20:11:28 +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)
|
|
|
|
// }
|
|
|
|
|
|
|
|
tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
|
2024-03-03 09:16:49 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-03-07 20:11:28 +01:00
|
|
|
tmpl.ExecuteTemplate(w, "page-content", article)
|
2024-03-01 11:30:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-09 10:25:20 +01:00
|
|
|
func PublishArticle(db *model.DB, s *control.CookieStore) http.HandlerFunc {
|
2024-02-27 14:10:27 +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-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)
|
|
|
|
}
|
|
|
|
|
2024-03-09 10:12:46 +01:00
|
|
|
db.UpdateAttribute("articles", id, "published", true)
|
2024-02-27 14:10:27 +01:00
|
|
|
|
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-02-27 14:10:27 +01:00
|
|
|
}
|
|
|
|
}
|