2024-07-13 13:58:36 +02:00
|
|
|
package frontend
|
2024-03-28 07:34:36 +01:00
|
|
|
|
|
|
|
import (
|
2024-08-30 21:20:29 +02:00
|
|
|
"fmt"
|
2024-03-28 07:34:36 +01:00
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2024-08-30 21:20:29 +02:00
|
|
|
"os"
|
|
|
|
"time"
|
2024-03-28 07:34:36 +01:00
|
|
|
|
2024-07-13 14:09:11 +02:00
|
|
|
b "streifling.com/jason/cpolis/cmd/backend"
|
2024-03-28 07:34:36 +01:00
|
|
|
)
|
|
|
|
|
2025-01-14 20:53:49 +01:00
|
|
|
func PublishLatestIssue(c *b.Config, db *b.DB, s map[string]*Session) http.HandlerFunc {
|
2024-03-28 07:34:36 +01:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2025-01-14 20:53:49 +01:00
|
|
|
session, err := ManageSession(w, r, c, s)
|
2024-08-18 11:20:06 +02:00
|
|
|
if err != nil {
|
2025-01-14 20:53:49 +01:00
|
|
|
http.Error(w, "Die Session ist abgelaufen. Bitte erneut anmelden.", http.StatusUnauthorized)
|
2024-08-30 21:20:29 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
article := &b.Article{
|
2024-10-27 07:20:23 +01:00
|
|
|
Title: r.PostFormValue("issue-title"),
|
2024-10-27 07:21:36 +01:00
|
|
|
BannerLink: r.PostFormValue("issue-banner-url"),
|
2024-09-01 18:18:07 +02:00
|
|
|
Published: true,
|
|
|
|
Rejected: false,
|
|
|
|
Created: time.Now(),
|
|
|
|
AutoGenerated: true,
|
2024-08-30 21:20:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-27 07:20:23 +01:00
|
|
|
if len(article.Title) == 0 {
|
|
|
|
http.Error(w, "Bitte den Titel eingeben.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2024-10-27 07:21:36 +01:00
|
|
|
|
2024-08-30 23:43:01 +02:00
|
|
|
article.ID, err = db.AddArticle(article)
|
2024-08-30 21:20:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-01 10:04:42 +01:00
|
|
|
authorIDs := make([]int64, 1)
|
2025-01-14 20:53:49 +01:00
|
|
|
authorIDs[0] = session.User.ID
|
2024-12-01 10:04:42 +01:00
|
|
|
|
|
|
|
if err = db.WriteArticleAuthors(article.ID, authorIDs); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-04 16:06:33 +02:00
|
|
|
content := []byte(r.PostFormValue("issue-content"))
|
|
|
|
if len(content) == 0 {
|
|
|
|
http.Error(w, "Bitte eine Beschreibung eingeben.", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-30 23:43:01 +02:00
|
|
|
articleAbsName := fmt.Sprint(c.ArticleDir, "/", article.ID, ".md")
|
2024-10-04 16:06:33 +02:00
|
|
|
if err = os.WriteFile(articleAbsName, content, 0644); err != nil {
|
2024-08-30 21:20:29 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-03-28 07:34:36 +01:00
|
|
|
if err := db.PublishLatestIssue(); err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 07:20:23 +01:00
|
|
|
feed, err := b.GenerateAtomFeed(c, db)
|
2024-08-30 21:20:29 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-10-28 17:57:51 +01:00
|
|
|
if err = b.SaveAtomFeed(c.AtomFile, feed); err != nil {
|
2024-08-30 21:20:29 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-27 13:58:19 +01:00
|
|
|
data := new(struct{ Role int })
|
2025-01-14 20:53:49 +01:00
|
|
|
data.Role = session.User.Role
|
2024-10-27 13:58:19 +01:00
|
|
|
|
2024-03-29 09:07:17 +01:00
|
|
|
tmpl, err := template.ParseFiles(c.WebDir + "/templates/hub.html")
|
2024-03-28 07:34:36 +01:00
|
|
|
tmpl = template.Must(tmpl, err)
|
2024-10-27 13:58:19 +01:00
|
|
|
if err = tmpl.ExecuteTemplate(w, "page-content", data); err != nil {
|
2024-10-04 10:21:56 +02:00
|
|
|
log.Println(err)
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2024-03-28 07:34:36 +01:00
|
|
|
}
|
|
|
|
}
|