Just a bit of cleaning up

This commit is contained in:
Jason Streifling 2024-03-02 09:09:55 +01:00
parent aa7fcd6075
commit 655992c8b2
5 changed files with 52 additions and 22 deletions

View File

@ -9,10 +9,10 @@ const (
type Role int type Role int
type User struct { type User struct {
RejectedArticles *ArticleList
UserName string UserName string
FirstName string FirstName string
LastName string LastName string
RejectedArticles []*Article
ID int64 ID int64
Role Role
} }

View File

@ -11,8 +11,8 @@ import (
) )
type AddUserData struct { type AddUserData struct {
data.User
Msg string Msg string
data.User
} }
func inputsEmpty(user data.User, pass, pass2 string) bool { func inputsEmpty(user data.User, pass, pass2 string) bool {
@ -46,16 +46,27 @@ func HomePage(db *data.DB) http.HandlerFunc {
} }
if numRows == 0 { if numRows == 0 {
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/add-user.html")).Execute(w, nil) files := []string{
"web/templates/index.html",
"web/templates/add-user.html",
}
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil)
} else { } else {
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/login.html")).Execute(w, nil) files := []string{
"web/templates/index.html",
"web/templates/login.html",
}
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, nil)
} }
} }
} }
func CreateUser() http.HandlerFunc { func CreateUser() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }
@ -81,24 +92,30 @@ func AddUser(db *data.DB) http.HandlerFunc {
if inputsEmpty(htmlData.User, pass, pass2) { if inputsEmpty(htmlData.User, pass, pass2) {
htmlData.Msg = "Alle Felder müssen ausgefüllt werden." htmlData.Msg = "Alle Felder müssen ausgefüllt werden."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
userString, stringLen, ok := checkUserStrings(htmlData.User) userString, stringLen, ok := checkUserStrings(htmlData.User)
if !ok { if !ok {
htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ", stringLen, " Zeichen erlaubt.") htmlData.Msg = fmt.Sprint(userString, " ist zu lang. Maximal ",
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) stringLen, " Zeichen erlaubt.")
tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
id, _ := db.GetID(htmlData.UserName) id, _ := db.GetID(htmlData.UserName)
if id != 0 { if id != 0 {
htmlData.Msg = fmt.Sprint(htmlData.UserName, " ist bereits vergeben. Bitte anderen Benutzernamen wählen.") htmlData.Msg = fmt.Sprint(htmlData.UserName,
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) " ist bereits vergeben. Bitte anderen Benutzernamen wählen.")
tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
if pass != pass2 { if pass != pass2 {
htmlData.Msg = "Die Passwörter stimmen nicht überein." htmlData.Msg = "Die Passwörter stimmen nicht überein."
template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", htmlData) tmpl, err := template.ParseFiles("web/templates/add-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", htmlData)
return return
} }
@ -107,6 +124,7 @@ func AddUser(db *data.DB) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }

View File

@ -13,13 +13,15 @@ import (
func ShowHub() http.HandlerFunc { func ShowHub() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }
func WriteArticle() http.HandlerFunc { func WriteArticle() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/editor.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }
@ -54,13 +56,15 @@ func FinishArticle(l *data.ArticleList) http.HandlerFunc {
l.Add(article) l.Add(article)
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }
func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc { func ShowUnpublishedArticles(l *data.ArticleList) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("web/templates/unpublished-articles.html")).ExecuteTemplate(w, "page-content", l.Get()) tmpl, err := template.ParseFiles("web/templates/unpublished-articles.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", l.Get())
} }
} }
@ -75,11 +79,13 @@ func ReviewArticle(l *data.ArticleList) http.HandlerFunc {
for _, article := range l.Get() { for _, article := range l.Get() {
if article.UUID == uuid { if article.UUID == uuid {
template.Must(template.ParseFiles("web/templates/to-be-published.html")).ExecuteTemplate(w, "page-content", article) tmpl, err := template.ParseFiles("web/templates/to-be-published.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", article)
return return
} }
} }
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }
@ -96,7 +102,8 @@ func PublishArticle(f *data.Feed, l *data.ArticleList) http.HandlerFunc {
if !ok { if !ok {
// TODO: Warnung anzeigen // TODO: Warnung anzeigen
// msg = "Alle Felder müssen ausgefüllt werden." // msg = "Alle Felder müssen ausgefüllt werden."
// template.Must(template.ParseFiles("web/templates/add-user.html")).ExecuteTemplate(w, "page-content", msg) // tmpl, err := template.ParseFiles("web/templates/add-user.html")
// template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
return return
} }
@ -108,6 +115,7 @@ func PublishArticle(f *data.Feed, l *data.ArticleList) http.HandlerFunc {
}) })
f.Save("tmp/rss.gob") f.Save("tmp/rss.gob")
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }

View File

@ -17,6 +17,9 @@ func ShowRSS(f *data.Feed) http.HandlerFunc {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
template.Must(template.ParseFiles("web/templates/index.html", "web/templates/feed.rss")).Execute(w, rss)
files := []string{"web/templates/index.html", "web/templates/feed.rss"}
tmpl, err := template.ParseFiles(files...)
template.Must(tmpl, err).Execute(w, rss)
} }
} }

View File

@ -26,6 +26,7 @@ func Login(db *data.DB) http.HandlerFunc {
return return
} }
template.Must(template.ParseFiles("web/templates/hub.html")).ExecuteTemplate(w, "page-content", nil) tmpl, err := template.ParseFiles("web/templates/hub.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", nil)
} }
} }