diff --git a/admin.go b/admin.go index 4d494c2..f0d8449 100644 --- a/admin.go +++ b/admin.go @@ -425,3 +425,86 @@ func AdminPositionsHandler(w http.ResponseWriter, r *http.Request) { return } } + +func AdminQRHandler(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 QR code + if r.PostForm.Has("delete") { + qrID := r.FormValue("delete") + _, err := db.Exec("DELETE FROM qr_codes WHERE id = ?", qrID) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/qr", http.StatusSeeOther) + return + } + // Adding a new QR code + positionID := r.FormValue("position") + uid := r.FormValue("uid") + if positionID == "" || uid == "" { + http.Error(w, "All fields are required", http.StatusBadRequest) + return + } + _, err := db.Exec("INSERT INTO qr_codes (position_id, uid) VALUES (?, ?)", positionID, uid) + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + http.Redirect(w, r, "/admin/qr", http.StatusSeeOther) + return + } + // Fetch all QR codes with their associated positions + rows, err := db.Query("SELECT qr_codes.id, qr_codes.uid, COALESCE(position_id, ''), COALESCE(gps, '') FROM qr_codes LEFT JOIN positions ON qr_codes.position_id = positions.id ORDER BY qr_codes.id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var qrs []AdminQRsTemplateS + for rows.Next() { + var qr AdminQRsTemplateS + if err := rows.Scan(&qr.ID, &qr.URL, &qr.Position, &qr.GPS); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + qr.URL = domain + "/qr/" + qr.URL + qrs = append(qrs, qr) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + // Fetch all positions for the dropdown + rows, err = db.Query("SELECT id FROM positions ORDER BY id") + if err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + defer rows.Close() + var positions []int + for rows.Next() { + var position int + if err := rows.Scan(&position); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + positions = append(positions, position) + } + if err := rows.Err(); err != nil { + http.Error(w, "Database error", http.StatusInternalServerError) + return + } + if err := AdminQRsTemplate.Execute(w, AdminQRTemplateS{QRs: qrs, Positions: positions}); err != nil { + http.Error(w, "Template error", http.StatusInternalServerError) + return + } +} diff --git a/klice.go b/klice.go index 4788bda..5d701cf 100644 --- a/klice.go +++ b/klice.go @@ -344,6 +344,7 @@ func main() { http.HandleFunc("/admin/levels", AdminLevelHandler) http.HandleFunc("/admin/cipher", AdminCipherHandler) http.HandleFunc("/admin/positions", AdminPositionsHandler) + http.HandleFunc("/admin/qr", AdminQRHandler) fmt.Println("Server started at :8080") http.ListenAndServe(":8080", nil) diff --git a/templates.go b/templates.go index 1d0130a..1d80a86 100644 --- a/templates.go +++ b/templates.go @@ -52,6 +52,18 @@ type AdminPositionsTemplateS struct { URL string } +type AdminQRsTemplateS struct { + URL string + Position string + GPS string + ID int +} + +type AdminQRTemplateS struct { + QRs []AdminQRsTemplateS + Positions []int +} + 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")) @@ -59,3 +71,4 @@ var AdminRoutesTemplate = template.Must(template.ParseFiles("templates/adminRout 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")) +var AdminQRsTemplate = template.Must(template.ParseFiles("templates/adminQR.html")) diff --git a/templates/adminPanel.html b/templates/adminPanel.html index 705d8e5..79dcd19 100644 --- a/templates/adminPanel.html +++ b/templates/adminPanel.html @@ -13,6 +13,7 @@ Úrovně
Šifry
Pozice
+ QR Kódy

diff --git a/templates/adminQR.html b/templates/adminQR.html new file mode 100644 index 0000000..bd1e3fe --- /dev/null +++ b/templates/adminQR.html @@ -0,0 +1,47 @@ + + + + + + QR + + + +

QR

+ + + + + + + + {{range .QRs}} + + + + + + + {{end}} +
URLPoziceGPSSmazat
{{.URL}}{{.Position}}{{.GPS}} + + + + +
+
+

Přidat nový QR kód

+
+ URL - jenom poslední část: + Pozice: + +
+
+ Zpět na admin panel + + + \ No newline at end of file