package handler import ( "net/http" "strconv" "evanpage-backend/internal/service" "github.com/gin-gonic/gin" ) type AdminHandler struct { userService *service.UserService } func NewAdminHandler(userService *service.UserService) *AdminHandler { return &AdminHandler{userService: userService} } func (h *AdminHandler) ListUsers(c *gin.Context) { users, err := h.userService.ListUsers() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"users": users}) } func (h *AdminHandler) CreateUser(c *gin.Context) { var req struct { Username string `json:"username" binding:"required"` Email string `json:"email" binding:"required,email"` Password string `json:"password" binding:"required,min=6"` Role string `json:"role" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } user, err := h.userService.Register(req.Username, req.Email, req.Password, req.Role) if err != nil { c.JSON(http.StatusConflict, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, gin.H{ "id": user.ID, "username": user.Username, "email": user.Email, "role": user.Role, }) } func (h *AdminHandler) DeleteUser(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"}) return } if err := h.userService.DeleteUser(uint(id)); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "user deleted"}) }