From f52674b179d376039b9ce106882c4b2c36492ddc Mon Sep 17 00:00:00 2001 From: Jason Streifling Date: Tue, 12 Mar 2024 19:56:22 +0100 Subject: [PATCH] Fixed dumb routing mistake --- cmd/main.go | 1 + cmd/model/db.go | 4 ++-- cmd/view/users.go | 43 +++++++++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 32410bc..97a0d4a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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)) } diff --git a/cmd/model/db.go b/cmd/model/db.go index 566bc95..e67b7a3 100644 --- a/cmd/model/db.go +++ b/cmd/model/db.go @@ -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) } } diff --git a/cmd/view/users.go b/cmd/view/users.go index d1d4000..acd1e9d 100644 --- a/cmd/view/users.go +++ b/cmd/view/users.go @@ -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)) } }