package frontend import ( "fmt" "html/template" "log" "net/http" "os" "time" b "streifling.com/jason/cpolis/cmd/backend" ) func PublishLatestIssue(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { session, err := GetSession(w, r, c, s) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } session.Values["article"] = nil if err = session.Save(r, w); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } article := &b.Article{ Title: r.PostFormValue("issue-title"), BannerLink: r.PostFormValue("issue-banner-url"), Published: true, Rejected: false, Created: time.Now(), AutoGenerated: true, } if len(article.Title) == 0 { http.Error(w, "Bitte den Titel eingeben.", http.StatusBadRequest) return } article.ID, err = db.AddArticle(article) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } authorIDs := make([]int64, 1) var ok bool if authorIDs[0], ok = session.Values["id"].(int64); !ok { msg := "fälschlicherweise session.Values[\"id\"].(int64) für authorIDs[0] angenommen" log.Println(msg) http.Error(w, msg, http.StatusInternalServerError) return } if err = db.WriteArticleAuthors(article.ID, authorIDs); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } content := []byte(r.PostFormValue("issue-content")) if len(content) == 0 { http.Error(w, "Bitte eine Beschreibung eingeben.", http.StatusBadRequest) return } articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md") if err = os.WriteFile(articleAbsName, content, 0644); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if err = db.AddArticleToCurrentIssue(article.ID); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := db.PublishLatestIssue(); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } feed, err := b.GenerateAtomFeed(c, db) if err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } if err = b.SaveAtomFeed(c.AtomFile, feed); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } session.Values["issue-image"] = nil if err = session.Save(r, w); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } data := new(struct{ Role int }) data.Role = session.Values["role"].(int) tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html") tmpl = template.Must(tmpl, err) if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusInternalServerError) return } } }