Add rules language support and improve password validation across the app

This commit is contained in:
Jan Meinl
2026-05-17 05:57:20 +02:00
parent 68034dea7d
commit 570272a777
16 changed files with 330 additions and 77 deletions
+27 -8
View File
@@ -43,9 +43,9 @@ func handleListCompetitions(w http.ResponseWriter, r *http.Request) {
var rows *sql.Rows
var err error
if u.IsSystemAdmin {
rows, err = db.Query("SELECT id,name,allow_any_scorer_edit,created_at FROM competitions ORDER BY created_at DESC")
rows, err = db.Query("SELECT id,name,allow_any_scorer_edit,rules_language,created_at FROM competitions ORDER BY created_at DESC")
} else {
rows, err = db.Query(`SELECT c.id,c.name,c.allow_any_scorer_edit,c.created_at,cu.role
rows, err = db.Query(`SELECT c.id,c.name,c.allow_any_scorer_edit,c.rules_language,c.created_at,cu.role
FROM competitions c JOIN competition_users cu ON cu.competition_id=c.id
WHERE cu.user_id=? ORDER BY c.created_at DESC`, u.ID)
}
@@ -59,10 +59,10 @@ func handleListCompetitions(w http.ResponseWriter, r *http.Request) {
var c Competition
var allow int
if u.IsSystemAdmin {
rows.Scan(&c.ID, &c.Name, &allow, &c.CreatedAt)
rows.Scan(&c.ID, &c.Name, &allow, &c.RulesLanguage, &c.CreatedAt)
c.Role = "system_admin"
} else {
rows.Scan(&c.ID, &c.Name, &allow, &c.CreatedAt, &c.Role)
rows.Scan(&c.ID, &c.Name, &allow, &c.RulesLanguage, &c.CreatedAt, &c.Role)
}
c.AllowAnyScorerEdit = allow == 1
out = append(out, c)
@@ -74,6 +74,7 @@ func handleCreateCompetition(w http.ResponseWriter, r *http.Request) {
var req struct {
Name string `json:"name"`
AllowAnyScorerEdit bool `json:"allow_any_scorer_edit"`
RulesLanguage string `json:"rules_language"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid_body")
@@ -91,16 +92,30 @@ func handleCreateCompetition(w http.ResponseWriter, r *http.Request) {
if req.AllowAnyScorerEdit {
allow = 1
}
res, err := db.Exec("INSERT INTO competitions(name,allow_any_scorer_edit) VALUES(?,?)", req.Name, allow)
lang := normalizeRulesLanguage(req.RulesLanguage)
res, err := db.Exec("INSERT INTO competitions(name,allow_any_scorer_edit,rules_language) VALUES(?,?,?)", req.Name, allow, lang)
if err != nil {
writeError(w, http.StatusInternalServerError, "db_error")
return
}
id, _ := res.LastInsertId()
c := Competition{ID: id, Name: req.Name, AllowAnyScorerEdit: req.AllowAnyScorerEdit}
c := Competition{ID: id, Name: req.Name, AllowAnyScorerEdit: req.AllowAnyScorerEdit, RulesLanguage: lang}
writeJSON(w, http.StatusCreated, c)
}
func normalizeRulesLanguage(lang string) string {
if lang == "" {
return "en"
}
rulesMu.RLock()
_, ok := rules[lang]
rulesMu.RUnlock()
if !ok {
return "en"
}
return lang
}
func handleGetCompetition(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
if err != nil {
@@ -115,8 +130,8 @@ func handleGetCompetition(w http.ResponseWriter, r *http.Request) {
}
var c Competition
var allow int
err = db.QueryRow("SELECT id,name,allow_any_scorer_edit,created_at FROM competitions WHERE id=?", id).
Scan(&c.ID, &c.Name, &allow, &c.CreatedAt)
err = db.QueryRow("SELECT id,name,allow_any_scorer_edit,rules_language,created_at FROM competitions WHERE id=?", id).
Scan(&c.ID, &c.Name, &allow, &c.RulesLanguage, &c.CreatedAt)
if err != nil {
writeError(w, http.StatusNotFound, "not_found")
return
@@ -141,6 +156,7 @@ func handleUpdateCompetition(w http.ResponseWriter, r *http.Request) {
var req struct {
Name *string `json:"name"`
AllowAnyScorerEdit *bool `json:"allow_any_scorer_edit"`
RulesLanguage *string `json:"rules_language"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
writeError(w, http.StatusBadRequest, "invalid_body")
@@ -156,6 +172,9 @@ func handleUpdateCompetition(w http.ResponseWriter, r *http.Request) {
}
db.Exec("UPDATE competitions SET allow_any_scorer_edit=? WHERE id=?", v, id)
}
if req.RulesLanguage != nil {
db.Exec("UPDATE competitions SET rules_language=? WHERE id=?", normalizeRulesLanguage(*req.RulesLanguage), id)
}
hub.broadcast(id, "competition_updated", nil)
w.WriteHeader(http.StatusNoContent)
}