Shorten lines by referencing frontend as f and backend as b

This commit is contained in:
2024-07-13 14:09:11 +02:00
parent d0c1e525d2
commit 85e2f8b4ad
7 changed files with 105 additions and 107 deletions

View File

@ -13,10 +13,10 @@ import (
"time"
"github.com/google/uuid"
"streifling.com/jason/cpolis/cmd/backend"
b "streifling.com/jason/cpolis/cmd/backend"
)
func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func ShowHub(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -30,7 +30,7 @@ func ShowHub(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.Han
}
}
func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
func WriteArticle(c *b.Config, db *b.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tags, err := db.GetTagList()
if err != nil {
@ -44,7 +44,7 @@ func WriteArticle(c *backend.Config, db *backend.DB) http.HandlerFunc {
}
}
func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func SubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -53,7 +53,7 @@ func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
article := &backend.Article{
article := &b.Article{
Title: r.PostFormValue("article-title"),
Description: r.PostFormValue("article-description"),
Content: r.PostFormValue("article-content"),
@ -92,7 +92,7 @@ func SubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
}
}
func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func ResubmitArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -106,10 +106,10 @@ func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore)
content := r.PostFormValue("article-content")
if err = db.UpdateAttributes(
&backend.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
&backend.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
&backend.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
&b.Attribute{Table: "articles", ID: id, AttName: "title", Value: title},
&b.Attribute{Table: "articles", ID: id, AttName: "description", Value: description},
&b.Attribute{Table: "articles", ID: id, AttName: "content", Value: content},
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -146,7 +146,7 @@ func ResubmitArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore)
}
}
func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
func ShowUnpublishedArticles(c *b.Config, db *b.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
unpublishedArticles, err := db.GetCertainArticles(false, false)
if err != nil {
@ -161,11 +161,11 @@ func ShowUnpublishedArticles(c *backend.Config, db *backend.DB) http.HandlerFunc
}
}
func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func ShowRejectedArticles(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
MyIDs map[int64]bool
RejectedArticles []*backend.Article
RejectedArticles []*b.Article
}
data := new(htmlData)
@ -196,13 +196,13 @@ func ShowRejectedArticles(c *backend.Config, db *backend.DB, s *backend.CookieSt
}
}
func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func ReviewUnpublishedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
Title string
Description string
Content template.HTML
Tags []*backend.Tag
Tags []*b.Tag
ID int64
}
@ -223,21 +223,21 @@ func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.Cook
return
}
data.Title, err = backend.ConvertToPlain(article.Title)
data.Title, err = b.ConvertToPlain(article.Title)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Description, err = backend.ConvertToPlain(article.Description)
data.Description, err = b.ConvertToPlain(article.Description)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
content, err := backend.ConvertToHTML(article.Content)
content, err := b.ConvertToHTML(article.Content)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -258,12 +258,12 @@ func ReviewUnpublishedArticle(c *backend.Config, db *backend.DB, s *backend.Cook
}
}
func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func ReviewRejectedArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
Selected map[int64]bool
Article *backend.Article
Tags []*backend.Tag
Article *b.Article
Tags []*b.Tag
}
data := new(htmlData)
@ -305,7 +305,7 @@ func ReviewRejectedArticle(c *backend.Config, db *backend.DB, s *backend.CookieS
}
}
func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func PublishArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -328,22 +328,22 @@ func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) h
}
if err = db.UpdateAttributes(
&backend.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
&backend.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
&b.Attribute{Table: "articles", ID: id, AttName: "published", Value: true},
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: false},
&b.Attribute{Table: "articles", ID: id, AttName: "created", Value: time.Now().Format("2006-01-02 15:04:05")},
); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
feed, err := backend.GenerateRSS(db, c.Title, c.Link, c.Description)
feed, err := b.GenerateRSS(db, c.Title, c.Link, c.Description)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err = backend.SaveRSS(c.RSSFile, feed); err != nil {
if err = b.SaveRSS(c.RSSFile, feed); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
@ -355,7 +355,7 @@ func PublishArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) h
}
}
func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) http.HandlerFunc {
func RejectArticle(c *b.Config, db *b.DB, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@ -372,7 +372,7 @@ func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
}
if err = db.UpdateAttributes(
&backend.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
&b.Attribute{Table: "articles", ID: id, AttName: "rejected", Value: true},
); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -385,7 +385,7 @@ func RejectArticle(c *backend.Config, db *backend.DB, s *backend.CookieStore) ht
}
}
func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
func ShowCurrentArticles(c *b.Config, db *b.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
articles, err := db.GetCurrentIssueArticles()
if err != nil {
@ -399,7 +399,7 @@ func ShowCurrentArticles(c *backend.Config, db *backend.DB) http.HandlerFunc {
}
}
func UploadImage(c *backend.Config) http.HandlerFunc {
func UploadImage(c *b.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
file, header, err := r.FormFile("article-image")
if err != nil {
@ -440,7 +440,7 @@ func UploadImage(c *backend.Config) http.HandlerFunc {
}
}
func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc {
func PreviewArticle(c *b.Config, s *b.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
type htmlData struct {
Title string
@ -451,21 +451,21 @@ func PreviewArticle(c *backend.Config, s *backend.CookieStore) http.HandlerFunc
var err error
data := new(htmlData)
data.Title, err = backend.ConvertToPlain(r.PostFormValue("article-title"))
data.Title, err = b.ConvertToPlain(r.PostFormValue("article-title"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data.Description, err = backend.ConvertToPlain(r.PostFormValue("article-description"))
data.Description, err = b.ConvertToPlain(r.PostFormValue("article-description"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
content, err := backend.ConvertToHTML(r.PostFormValue("article-content"))
content, err := b.ConvertToHTML(r.PostFormValue("article-content"))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)