diff --git a/admin.go b/admin.go index 5051994..0084fc4 100644 --- a/admin.go +++ b/admin.go @@ -183,3 +183,63 @@ func AdminRouteHandler(w http.ResponseWriter, r *http.Request) { return } } + +func AdminLevelHandler(w http.ResponseWriter, r *http.Request) { + if !isAdmin(r) { + http.Redirect(w, r, "/admin/login", http.StatusSeeOther) + return + } + if r.Method == http.MethodPost { + if err := r.ParseForm(); err != nil { + http.Error(w, "Error parsing form", http.StatusBadRequest) + return + } + // Deleting an existing difficulty level + if r.PostForm.Has("delete") { + levelName := r.FormValue("delete") + _, err := db.Exec("DELETE FROM difficulty_levels WHERE level_name = ?", levelName) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/levels", http.StatusSeeOther) + return + } + // Adding a new difficulty level + levelName := r.FormValue("name") + if levelName == "" { + http.Error(w, "Level name cannot be empty", http.StatusBadRequest) + return + } + _, err := db.Exec("INSERT INTO difficulty_levels (level_name) VALUES (?)", levelName) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/levels", http.StatusSeeOther) + return + } + rows, err := db.Query("SELECT level_name FROM difficulty_levels ORDER BY id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var difficultyLevels []string + for rows.Next() { + var level string + if err := rows.Scan(&level); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + difficultyLevels = append(difficultyLevels, level) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + if err := AdminLevelTemplate.Execute(w, difficultyLevels); err != nil { + http.Error(w, "Template error", http.StatusInternalServerError) + return + } +} diff --git a/klice.go b/klice.go index 8aabe0e..e3fb75b 100644 --- a/klice.go +++ b/klice.go @@ -339,6 +339,7 @@ func main() { http.HandleFunc("/admin/teams", adminTeamsHandler) http.HandleFunc("/admin/start", AdminStartHandler) http.HandleFunc("/admin/routes", AdminRouteHandler) + http.HandleFunc("/admin/levels", AdminLevelHandler) fmt.Println("Server started at :8080") http.ListenAndServe(":8080", nil) diff --git a/templates.go b/templates.go index 32515df..a375ed9 100644 --- a/templates.go +++ b/templates.go @@ -32,3 +32,4 @@ var CipherTemplate = template.Must(template.ParseFiles("templates/assignment.htm var TeamTemplate = template.Must(template.ParseFiles("templates/team.html")) var AdminTeamsTemplate = template.Must(template.ParseFiles("templates/adminTeams.html")) var AdminRoutesTemplate = template.Must(template.ParseFiles("templates/adminRoutes.html")) +var AdminLevelTemplate = template.Must(template.ParseFiles("templates/adminLevels.html")) diff --git a/templates/adminLevels.html b/templates/adminLevels.html new file mode 100644 index 0000000..98452f4 --- /dev/null +++ b/templates/adminLevels.html @@ -0,0 +1,36 @@ + + + + + + Úrovně obtížnosti + + + +

Úrovně obtížnosti

+ + + + + + {{range .}} + + + + + {{end}} +
JménoSmazat
{{.}} +
+ + +
+
+
+

Přidat novou úroveň

+
+ Jméno: + +
+ + + \ No newline at end of file diff --git a/templates/adminPanel.html b/templates/adminPanel.html index 709bf1e..080751d 100644 --- a/templates/adminPanel.html +++ b/templates/adminPanel.html @@ -10,6 +10,7 @@

Admin Panel

Týmy
Trasy
+ Úrovně