package view import ( "html/template" "log" "net/http" "strconv" "streifling.com/jason/cpolis/cmd/control" "streifling.com/jason/cpolis/cmd/model" ) func ShowHub(s *control.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 *model.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 *model.DB, s *control.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { article := new(model.Article) var err error article.Title, err = control.ConvertToPlain(r.PostFormValue("editor-title")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } article.Desc, err = control.ConvertToPlain(r.PostFormValue("editor-desc")) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } article.Content, err = control.ConvertToHTML(r.PostFormValue("editor-text")) 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) } article.AuthorID = session.Values["id"].(int64) 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 } tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) } } func ShowUnpublishedArticles(db *model.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { articles, err := db.GetCertainArticles(false) 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 *model.DB, s *control.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/to-be-published.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", article) } } func PublishArticle(db *model.DB, s *control.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 } 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) } db.UpdateAttribute("articles", id, "published", true) tmpl, err := template.ParseFiles("web/templates/hub.html") tmpl = template.Must(tmpl, err) tmpl.ExecuteTemplate(w, "page-content", session.Values["role"]) } }