101 lines
2.5 KiB
Go
101 lines
2.5 KiB
Go
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 map[string]*Session) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, err := ManageSession(w, r, c, s)
|
|
if err != nil {
|
|
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
|
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)
|
|
authorIDs[0] = session.User.ID
|
|
|
|
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
|
|
}
|
|
|
|
data := new(struct{ Role int })
|
|
data.Role = session.User.Role
|
|
|
|
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
|
|
}
|
|
}
|
|
}
|