feat: add tournoment

This commit is contained in:
2026-07-07 15:17:33 +03:30
parent bcfaf35d73
commit e58c50d6e5
10 changed files with 744 additions and 1 deletions
+65
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"net/http"
"strconv"
"hakemsho/internal/auth"
"hakemsho/internal/httpx"
@@ -197,6 +198,70 @@ func (h *Handler) BuyChatPack(w http.ResponseWriter, r *http.Request) {
}
}
// Tournaments — GET /api/tournaments (فهرست با وضعیتِ عضویتِ کاربر)
func (h *Handler) Tournaments(w http.ResponseWriter, r *http.Request) {
id, ok := uid(r)
if !ok {
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
return
}
list, err := h.svc.ListTournaments(r.Context(), id)
if err != nil {
httpx.Error(w, http.StatusInternalServerError, "server error")
return
}
httpx.JSON(w, http.StatusOK, map[string]any{"tournaments": list})
}
// TournamentStandings — GET /api/tournaments/standings?id=X (رده‌بندی)
func (h *Handler) TournamentStandings(w http.ResponseWriter, r *http.Request) {
if _, ok := uid(r); !ok {
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
return
}
tid, _ := strconv.ParseInt(r.URL.Query().Get("id"), 10, 64)
if tid == 0 {
httpx.Error(w, http.StatusBadRequest, "invalid id")
return
}
rows, err := h.svc.TournamentStandings(r.Context(), tid, 50)
if err != nil {
httpx.Error(w, http.StatusInternalServerError, "server error")
return
}
httpx.JSON(w, http.StatusOK, map[string]any{"standings": rows})
}
// JoinTournament — POST /api/tournaments/join {id}
func (h *Handler) JoinTournament(w http.ResponseWriter, r *http.Request) {
id, ok := uid(r)
if !ok {
httpx.Error(w, http.StatusUnauthorized, "unauthorized")
return
}
var req struct {
ID int64 `json:"id"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
httpx.Error(w, http.StatusBadRequest, "invalid body")
return
}
switch err := h.svc.JoinTournament(r.Context(), id, req.ID); {
case err == nil:
httpx.JSON(w, http.StatusOK, map[string]string{"message": "ثبت‌نام انجام شد"})
case errors.Is(err, ErrTournamentNotFound):
httpx.Error(w, http.StatusNotFound, "تورنومنت یافت نشد")
case errors.Is(err, ErrTournamentClosed):
httpx.Error(w, http.StatusConflict, "مهلتِ ثبت‌نام تمام شده")
case errors.Is(err, ErrAlreadyJoined):
httpx.Error(w, http.StatusConflict, "قبلاً ثبت‌نام کرده‌اید")
case errors.Is(err, ErrInsufficient):
httpx.Error(w, http.StatusPaymentRequired, "insufficient coins")
default:
httpx.Error(w, http.StatusInternalServerError, "server error")
}
}
// SelectCard — POST /api/shop/select-card
func (h *Handler) SelectCard(w http.ResponseWriter, r *http.Request) {
id, ok := uid(r)