feat: add tournoment
This commit is contained in:
@@ -64,6 +64,9 @@ func (h *Handler) Routes(user, pass string) http.Handler {
|
||||
r.Post("/chat/message/add", h.chatMessageAdd)
|
||||
r.Post("/chat/message", h.chatMessageUpdate)
|
||||
r.Post("/chat/message/delete", h.chatMessageDelete)
|
||||
r.Get("/tournaments", h.tournamentsAdmin)
|
||||
r.Post("/tournaments/add", h.tournamentAdd)
|
||||
r.Post("/tournaments/delete", h.tournamentDelete)
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -681,3 +684,63 @@ func (h *Handler) rows(ctx context.Context, query string, cols ...string) []map[
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// --- مدیریتِ تورنومنتها ---
|
||||
|
||||
func (h *Handler) tournamentsAdmin(w http.ResponseWriter, r *http.Request) {
|
||||
list, _ := h.eco.AdminListTournaments(r.Context())
|
||||
h.render(w, "tournaments.html", map[string]any{
|
||||
"Nav": "tournaments",
|
||||
"Tournaments": list,
|
||||
"Saved": r.URL.Query().Get("saved") == "1",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) tournamentAdd(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
num := func(k string) int64 { v, _ := strconv.ParseInt(r.FormValue(k), 10, 64); return v }
|
||||
// جوایز: مقادیرِ سکه با کاما جدا شده ("۵۰۰۰,۳۰۰۰,۱۰۰۰").
|
||||
var prizes []int64
|
||||
for _, p := range strings.Split(r.FormValue("prizes"), ",") {
|
||||
p = strings.TrimSpace(p)
|
||||
if p == "" {
|
||||
continue
|
||||
}
|
||||
if v, err := strconv.ParseInt(p, 10, 64); err == nil {
|
||||
prizes = append(prizes, v)
|
||||
}
|
||||
}
|
||||
// ورودیِ datetime-local به وقتِ تهران (UTC+3:30) تفسیر و به UTC ذخیره میشود.
|
||||
iran := time.FixedZone("IRST", 12600)
|
||||
parseDT := func(k string) time.Time {
|
||||
t, err := time.ParseInLocation("2006-01-02T15:04", r.FormValue(k), iran)
|
||||
if err != nil {
|
||||
return time.Now().UTC()
|
||||
}
|
||||
return t.UTC()
|
||||
}
|
||||
if r.FormValue("title") == "" {
|
||||
http.Error(w, "عنوان لازم است", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
_, err := h.eco.CreateTournament(r.Context(), economy.Tournament{
|
||||
Title: r.FormValue("title"),
|
||||
Description: r.FormValue("description"),
|
||||
EntryFee: num("entry_fee"),
|
||||
Prizes: prizes,
|
||||
StartsAt: parseDT("starts_at"),
|
||||
EndsAt: parseDT("ends_at"),
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/admin/tournaments?saved=1", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (h *Handler) tournamentDelete(w http.ResponseWriter, r *http.Request) {
|
||||
_ = r.ParseForm()
|
||||
id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64)
|
||||
_ = h.eco.DeleteTournament(r.Context(), id)
|
||||
http.Redirect(w, r, "/admin/tournaments?saved=1", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user