add admin teams view
This commit is contained in:
62
admin.go
62
admin.go
@@ -47,7 +47,7 @@ func adminLogoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func isAdmin(r *http.Request) bool {
|
||||
func isAdmin(w http.ResponseWriter, r *http.Request) bool {
|
||||
cookie, err := r.Cookie("admin_session")
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -65,13 +65,65 @@ func isAdmin(r *http.Request) bool {
|
||||
username = matches[1]
|
||||
passwordHash = matches[2]
|
||||
err = db.QueryRow("SELECT 1 FROM admins WHERE username=? AND PASSWORD=?", username, passwordHash).Scan(new(int))
|
||||
return err != sql.ErrNoRows && err == nil
|
||||
if err != sql.ErrNoRows && err == nil {
|
||||
return true
|
||||
}
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
return false
|
||||
}
|
||||
|
||||
func adminHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAdmin(r) {
|
||||
http.Redirect(w, r, "/admin/login", http.StatusSeeOther)
|
||||
if !isAdmin(w, r) {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
w.Write([]byte("Welcome to the admin panel!"))
|
||||
http.ServeFile(w, r, "templates/adminPanel.html")
|
||||
}
|
||||
|
||||
func adminTeamsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAdmin(w, r) {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
rows, err := db.Query("SELECT name, difficulty_levels.level_name, last_cipher, penalty FROM teams JOIN difficulty_levels ON teams.difficulty_level = difficulty_levels.id ORDER BY name")
|
||||
if err != nil {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
var teams []TeamTemplateS
|
||||
for rows.Next() {
|
||||
var team TeamTemplateS
|
||||
if err := rows.Scan(&team.TeamName, &team.Difficulty, &team.LastCipher, &team.Penalties); err != nil {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
teams = append(teams, team)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if err := AdminTeamsTemplate.Execute(w, teams); err != nil {
|
||||
http.Error(w, "Template error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func AdminStartHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAdmin(w, r) {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
_, err := db.Exec("UPDATE teams SET last_cipher = 1, penalty = 0")
|
||||
if err != nil {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, err = db.Exec("DELETE FROM penalties")
|
||||
if err != nil {
|
||||
http.Error(w, "Database error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/admin/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
13
klice.go
13
klice.go
@@ -6,9 +6,7 @@ import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -58,14 +56,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
case http.MethodGet:
|
||||
loginPage, err := os.Open("templates/login.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Could not open login page", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer loginPage.Close()
|
||||
|
||||
io.Copy(w, loginPage)
|
||||
http.ServeFile(w, r, "templates/login.html")
|
||||
default:
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
}
|
||||
@@ -345,6 +336,8 @@ func main() {
|
||||
http.HandleFunc("/admin/login", adminLoginHandler)
|
||||
http.HandleFunc("/admin/logout", adminLogoutHandler)
|
||||
http.HandleFunc("/admin/", adminHandler)
|
||||
http.HandleFunc("/admin/teams", adminTeamsHandler)
|
||||
http.HandleFunc("/admin/start", AdminStartHandler)
|
||||
|
||||
fmt.Println("Server started at :8080")
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
||||
@@ -24,3 +24,4 @@ type TeamTemplateS struct {
|
||||
|
||||
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"))
|
||||
|
||||
19
templates/adminPanel.html
Normal file
19
templates/adminPanel.html
Normal file
@@ -0,0 +1,19 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="cs">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Admin Panel</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Admin Panel</h1>
|
||||
<a href="/admin/teams">Týmy</a>
|
||||
<a href="/admin/routes">Trasy</a>
|
||||
<hr>
|
||||
<form method="post" action="/admin/logout">
|
||||
<input type="submit" value="Logout">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
34
templates/adminTeams.html
Normal file
34
templates/adminTeams.html
Normal file
@@ -0,0 +1,34 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="cs">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Admin Panel</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Týmy</h1>
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>Název týmu</th>
|
||||
<th>Obtížnost</th>
|
||||
<th>Poslední šifra</th>
|
||||
<th>Penalizace (minuty)</th>
|
||||
</tr>
|
||||
{{range .}}
|
||||
<tr>
|
||||
<td>{{.TeamName}}</td>
|
||||
<td>{{.Difficulty}}</td>
|
||||
<td>{{.LastCipher}}</td>
|
||||
<td>{{.Penalties}}</td>
|
||||
</tr>
|
||||
{{end}}
|
||||
</table>
|
||||
<hr>
|
||||
<p>Vynulování penalizací a posledních šifer.</p>
|
||||
<a href="/admin/start">Start Závodu</a>
|
||||
<hr>
|
||||
<a href="/admin">Zpět na admin panel</a>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user