From 1742f65fcd6fc2f69e72f31f563232d31aff3877 Mon Sep 17 00:00:00 2001 From: Daniel Eder Date: Tue, 18 Feb 2025 09:16:58 +0100 Subject: [PATCH] add trainings --- main.go | 46 +++++++++++++++++++++++++++++++++--- templates/add_training.html | 27 +++++++++++++++++++++ templates/training_list.html | 3 +++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 templates/add_training.html diff --git a/main.go b/main.go index 366f25e..3ff2782 100644 --- a/main.go +++ b/main.go @@ -242,7 +242,7 @@ func addTrainingAreaHandler(w http.ResponseWriter, r *http.Request) { func fetchTrainings() ([]Training, error) { log.Println("Fetching trainings from the database") - rows, err := db.Query("SELECT id, title, training_type, mode, start, end, status FROM trainings") + rows, err := db.Query("SELECT id, title, training_type, mode, strftime('%Y-%m-%d %H:%M:%S', start), strftime('%Y-%m-%d %H:%M:%S', end), status FROM trainings") if err != nil { log.Printf("Error fetching trainings: %v", err) return nil, err @@ -258,8 +258,8 @@ func fetchTrainings() ([]Training, error) { log.Printf("Error scanning row for training: %v", err) return nil, err } - t.Start, _ = time.Parse("2006-01-02 15:04:05", start) - t.End, _ = time.Parse("2006-01-02 15:04:05", end) + t.Start, _ = time.Parse(time.DateTime, start) + t.End, _ = time.Parse(time.DateTime, end) trainings = append(trainings, t) } @@ -525,9 +525,49 @@ func logoutHandler(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/login", http.StatusSeeOther) } +func addTrainingHandler(w http.ResponseWriter, r *http.Request) { + log.Println("Handling request to add a new training") + + if r.Method == http.MethodPost { + title := r.FormValue("title") + trainingType := r.FormValue("training_type") + mode := r.FormValue("mode") + start := r.FormValue("start") + end := r.FormValue("end") + status := r.FormValue("status") + + if title == "" || trainingType == "" || mode == "" || start == "" || end == "" || status == "" { + log.Println("Missing required training fields") + http.Error(w, "All fields are required", http.StatusBadRequest) + return + } + + _, err := db.Exec("INSERT INTO trainings (title, training_type, mode, start, end, status) VALUES (?, ?, ?, ?, ?, ?)", + title, trainingType, mode, start, end, status) + if err != nil { + log.Printf("Error adding training: %v", err) + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + + log.Println("New training added successfully") + http.Redirect(w, r, "/trainings", http.StatusSeeOther) + return + } + + err := tmpl.ExecuteTemplate(w, "add_training.html", nil) + if err != nil { + log.Printf("Error rendering add_training.html: %v", err) + http.Error(w, "Template error", http.StatusInternalServerError) + } +} + func initServerHandlers() { http.Handle("/", redirectIfNoAdmin(http.HandlerFunc(homeHandler))) + http.Handle("/trainings", redirectIfNoAdmin(http.HandlerFunc(trainingListHandler))) + http.Handle("/trainings/add", redirectIfNoAdmin(http.HandlerFunc(addTrainingHandler))) + http.Handle("/trainers", redirectIfNoAdmin(http.HandlerFunc(trainerListHandler))) http.Handle("/trainers/add", redirectIfNoAdmin(http.HandlerFunc(addTrainerHandler))) http.Handle("/training-areas", redirectIfNoAdmin(http.HandlerFunc(trainingAreaListHandler))) diff --git a/templates/add_training.html b/templates/add_training.html new file mode 100644 index 0000000..baac200 --- /dev/null +++ b/templates/add_training.html @@ -0,0 +1,27 @@ +{{ template "preamble.html" }} +

Add Training

+
+
+
+
+
+
+
+ +
+{{ template "epilogue.html" }} \ No newline at end of file diff --git a/templates/training_list.html b/templates/training_list.html index 37139ec..478def2 100644 --- a/templates/training_list.html +++ b/templates/training_list.html @@ -1,4 +1,6 @@ {{ template "preamble.html" }} +

+

Trainings

@@ -20,4 +22,5 @@ {{ end }}
+ {{template "epilogue.html" }} \ No newline at end of file