Fixed dumb routing mistake

This commit is contained in:
Jason Streifling 2024-03-12 19:56:22 +01:00
parent 697939a17a
commit f52674b179
3 changed files with 36 additions and 12 deletions

View File

@ -68,6 +68,7 @@ func main() {
mux.HandleFunc("POST /review-rejected-article/", view.ReviewRejectedArticle(db, store))
mux.HandleFunc("POST /review-unpublished-article/", view.ReviewUnpublishedArticle(db, store))
mux.HandleFunc("POST /submit-article/", view.SubmitArticle(db, store))
mux.HandleFunc("POST /update-user/", view.UpdateUser(db, store))
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -107,7 +107,7 @@ func (db *DB) UpdateAttributes(a ...*Attribute) error {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
}
return fmt.Errorf("error updating article in DB: %v", err)
return fmt.Errorf("error updating %v in DB: %v", attribute.AttName, err)
}
}
@ -165,7 +165,7 @@ func (tx *Tx) UpdateAttributes(a ...*Attribute) error {
if rollbackErr := tx.Rollback(); rollbackErr != nil {
log.Fatalf("error: transaction error: %v, rollback error: %v", err, rollbackErr)
}
return fmt.Errorf("error updating article in DB: %v", err)
return fmt.Errorf("error updating %v in DB: %v", attribute.AttName, err)
}
}

View File

@ -121,6 +121,27 @@ func AddUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
}
func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
tmpl, err := template.ParseFiles("web/templates/login.html")
msg := "Session nicht mehr gültig. Bitte erneut anmelden."
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", msg)
}
user, err := db.GetUser(session.Values["id"].(int64))
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.ParseFiles("web/templates/edit-user.html")
template.Must(tmpl, err).ExecuteTemplate(w, "page-content", user)
}
}
func UpdateUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
session, err := s.Get(r, "cookie")
if err != nil {
@ -160,13 +181,15 @@ func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
return
}
if _, ok := db.GetID(userData.UserName); ok {
userData.Msg = "Benutzername bereits vergeben."
userData.UserName = ""
tmpl, err := template.ParseFiles("web/templates/edit-user.html")
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", userData)
return
if id, ok := db.GetID(userData.UserName); ok {
if id != userData.ID {
userData.Msg = "Benutzername bereits vergeben."
userData.UserName = ""
tmpl, err := template.ParseFiles("web/templates/edit-user.html")
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", userData)
return
}
}
tx, err := db.StartTransaction()
@ -198,8 +221,8 @@ func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
if err = tx.UpdateAttributes(
&model.Attribute{Table: "users", ID: userData.ID, AttName: "username", Value: userData.UserName},
&model.Attribute{Table: "users", ID: userData.ID, AttName: "first-name", Value: userData.FirstName},
&model.Attribute{Table: "users", ID: userData.ID, AttName: "last-name", Value: userData.LastName},
&model.Attribute{Table: "users", ID: userData.ID, AttName: "first_name", Value: userData.FirstName},
&model.Attribute{Table: "users", ID: userData.ID, AttName: "last_name", Value: userData.LastName},
); err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
@ -214,6 +237,6 @@ func EditUser(db *model.DB, s *control.CookieStore) http.HandlerFunc {
tmpl, err := template.ParseFiles("web/templates/hub.html")
tmpl = template.Must(tmpl, err)
tmpl.ExecuteTemplate(w, "page-content", session.Values["Role"].(string))
tmpl.ExecuteTemplate(w, "page-content", session.Values["role"].(int))
}
}