package ui import ( "html/template" "log" "net/http" "strconv" "time" "git.streifling.com/jason/rss" "streifling.com/jason/cpolis/cmd/data" ) func ShowHub(s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { 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/hub.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", session.Values["role"]) } } func WriteArticle(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { tags, err := db.GetTagList() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles("web/templates/editor.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", tags) } } func FinishArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { 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 } r.ParseForm() article.Tags = append(article.Tags, r.Form["tags"]...) 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) db.AddArticle(article) tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) } } func ShowUnpublishedArticles(db *data.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { articles, err := db.GetUnpublishedArticles() if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html") template.Must(tmpl, err).ExecuteTemplate(w, "page-content", articles) } } func ReviewArticle(db *data.DB, s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } 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 } 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/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) } } func PublishArticle(db *data.DB, c *data.Channel, s *data.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PostFormValue("id"), 10, 64) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } 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 } 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}, Categories: article.Tags, }) c.Save("tmp/rss.gob") tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) } }