feat(admin): added admin panel

This commit is contained in:
2025-06-18 17:29:01 +03:00
parent a157fc1cc3
commit 2c682c5123
5 changed files with 119 additions and 16 deletions

View File

@@ -0,0 +1,40 @@
package api
import (
"encoding/json"
"net/http"
"strconv"
"gitea.mrixs.me/minecraft-platform/backend/internal/database"
"github.com/go-chi/chi/v5"
)
type AdminUserHandler struct {
UserRepo *database.UserRepository
}
func (h *AdminUserHandler) GetAllUsers(w http.ResponseWriter, r *http.Request) {
users, err := h.UserRepo.GetAllUsers(r.Context())
if err != nil {
http.Error(w, "Failed to get users", http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(users)
}
func (h *AdminUserHandler) UpdateUserRole(w http.ResponseWriter, r *http.Request) {
userID, _ := strconv.Atoi(chi.URLParam(r, "id"))
var payload struct {
Role string `json:"role"`
}
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
// TODO: Валидация роли
if err := h.UserRepo.UpdateUserRole(r.Context(), userID, payload.Role); err != nil {
http.Error(w, "Failed to update role", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}