41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
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)
|
|
}
|