From 68c081891fe21751074cac3af10b5a8d8bfb201f Mon Sep 17 00:00:00 2001 From: prokopparuzek Date: Thu, 9 Oct 2025 17:25:25 +0200 Subject: [PATCH] admin: sifry --- admin.go | 62 +++++++++++++++++++++++++++++++++++++ klice.go | 1 + templates.go | 8 +++++ templates/adminCiphers.html | 49 +++++++++++++++++++++++++++++ templates/adminPanel.html | 1 + 5 files changed, 121 insertions(+) create mode 100644 templates/adminCiphers.html diff --git a/admin.go b/admin.go index 771190f..0c7f284 100644 --- a/admin.go +++ b/admin.go @@ -301,3 +301,65 @@ func AdminLevelHandler(w http.ResponseWriter, r *http.Request) { return } } + +func AdminCipherHandler(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 cipher + if r.PostForm.Has("delete") { + cipherID := r.FormValue("delete") + _, err := db.Exec("DELETE FROM ciphers WHERE id = ?", cipherID) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/cipher", http.StatusSeeOther) + return + } + // Adding a new cipher + assignment := r.FormValue("assignment") + solution := r.FormValue("solution") + clue := r.FormValue("clue") + if assignment == "" || solution == "" || clue == "" { + http.Error(w, "All fields are required", http.StatusBadRequest) + return + } + _, err := db.Exec("INSERT INTO ciphers (assignment, solution, clue) VALUES (?, ?, ?)", assignment, solution, clue) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/cipher", http.StatusSeeOther) + return + } + rows, err := db.Query("SELECT id, assignment, solution, clue FROM ciphers ORDER BY id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var ciphers []AdminCipherTemplateS + for rows.Next() { + var cipher AdminCipherTemplateS + if err := rows.Scan(&cipher.ID, &cipher.Assignment, &cipher.Solution, &cipher.Clue); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + ciphers = append(ciphers, cipher) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + if err := AdminCipherTemplate.Execute(w, ciphers); err != nil { + http.Error(w, "Template error", http.StatusInternalServerError) + return + } +} diff --git a/klice.go b/klice.go index fa7c72a..59d62a5 100644 --- a/klice.go +++ b/klice.go @@ -340,6 +340,7 @@ func main() { http.HandleFunc("/admin/start", AdminStartHandler) http.HandleFunc("/admin/routes", AdminRouteHandler) http.HandleFunc("/admin/levels", AdminLevelHandler) + http.HandleFunc("/admin/cipher", AdminCipherHandler) fmt.Println("Server started at :8080") http.ListenAndServe(":8080", nil) diff --git a/templates.go b/templates.go index cb0b393..0c66991 100644 --- a/templates.go +++ b/templates.go @@ -38,8 +38,16 @@ type AdminRoutesTemplateS struct { Ciphers []CipherTemplateS } +type AdminCipherTemplateS struct { + ID int + Assignment string + Solution string + Clue string +} + var CipherTemplate = template.Must(template.ParseFiles("templates/assignment.html")) 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")) +var AdminCipherTemplate = template.Must(template.ParseFiles("templates/adminCiphers.html")) diff --git a/templates/adminCiphers.html b/templates/adminCiphers.html new file mode 100644 index 0000000..f95e874 --- /dev/null +++ b/templates/adminCiphers.html @@ -0,0 +1,49 @@ + + + + + + Šifry + + + +

Šifry

+ + + + + + + + + {{range .}} + + + + + + + + {{end}} +
IDZadáníŘešeníNápovědaSmazat
{{.ID}}{{.Assignment}}{{.Solution}}{{.Clue}} +
+ + +
+
+
+

Nová šifra

+
+ + + + + + + +
+
+ Zpět na admin panel + + + \ No newline at end of file diff --git a/templates/adminPanel.html b/templates/adminPanel.html index 080751d..fd7c62d 100644 --- a/templates/adminPanel.html +++ b/templates/adminPanel.html @@ -11,6 +11,7 @@ Týmy
Trasy
Úrovně
+ Šifry