diff --git a/admin.go b/admin.go index 0c7f284..4d494c2 100644 --- a/admin.go +++ b/admin.go @@ -363,3 +363,65 @@ func AdminCipherHandler(w http.ResponseWriter, r *http.Request) { return } } + +func AdminPositionsHandler(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 position + if r.PostForm.Has("delete") { + positionID := r.FormValue("delete") + _, err := db.Exec("DELETE FROM positions WHERE id = ?", positionID) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/positions", http.StatusSeeOther) + return + } + // Adding a new position + gps := r.FormValue("gps") + clue := r.FormValue("clue") + if gps == "" || clue == "" { + http.Error(w, "All fields are required", http.StatusBadRequest) + return + } + _, err := db.Exec("INSERT INTO positions (gps, clue) VALUES (?, ?)", gps, clue) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/positions", http.StatusSeeOther) + return + } + rows, err := db.Query("SELECT positions.id, gps, clue, COALESCE(uid, '') FROM positions LEFT JOIN QR_CODES ON positions.id = QR_CODES.position_id ORDER BY positions.id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var positions []AdminPositionsTemplateS + for rows.Next() { + var position AdminPositionsTemplateS + if err := rows.Scan(&position.ID, &position.GPS, &position.Clue, &position.URL); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + position.URL = domain + "/qr/" + position.URL + positions = append(positions, position) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + if err := AdminPositionsTemplate.Execute(w, positions); err != nil { + http.Error(w, "Template error", http.StatusInternalServerError) + return + } +} diff --git a/klice.go b/klice.go index 59d62a5..4788bda 100644 --- a/klice.go +++ b/klice.go @@ -12,6 +12,8 @@ import ( _ "github.com/mattn/go-sqlite3" ) +const domain = "http://localhost:8080" + var db *sql.DB func hashPassword(password string) string { @@ -341,6 +343,7 @@ func main() { http.HandleFunc("/admin/routes", AdminRouteHandler) http.HandleFunc("/admin/levels", AdminLevelHandler) http.HandleFunc("/admin/cipher", AdminCipherHandler) + http.HandleFunc("/admin/positions", AdminPositionsHandler) fmt.Println("Server started at :8080") http.ListenAndServe(":8080", nil) diff --git a/templates.go b/templates.go index 0c66991..1d0130a 100644 --- a/templates.go +++ b/templates.go @@ -45,9 +45,17 @@ type AdminCipherTemplateS struct { Clue string } +type AdminPositionsTemplateS struct { + ID int + GPS string + Clue string + URL 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")) +var AdminPositionsTemplate = template.Must(template.ParseFiles("templates/adminPositions.html")) diff --git a/templates/adminPanel.html b/templates/adminPanel.html index fd7c62d..705d8e5 100644 --- a/templates/adminPanel.html +++ b/templates/adminPanel.html @@ -12,6 +12,7 @@ Trasy
Úrovně
Šifry
+ Pozice

diff --git a/templates/adminPositions.html b/templates/adminPositions.html new file mode 100644 index 0000000..4e44f8f --- /dev/null +++ b/templates/adminPositions.html @@ -0,0 +1,47 @@ + + + + + + Pozice + + + +

Pozice

+ + + + + + + + + {{range .}} + + + + + + + + {{end}} +
IDGPSNápovědaURLSmazat
{{.ID}}{{.GPS}}{{.Clue}}{{.URL}} + + + + +
+
+

Přidat pozici

+
+ +
+ +
+ +
+
+ Zpět na admin panel + + + \ No newline at end of file