package handlers import ( "gobeyhan/app/account/services" "gobeyhan/database/models" "net/http" "strconv" "github.com/gin-gonic/gin" ) type RoleHandler struct { service *services.RoleService } func NewRoleHandler(service *services.RoleService) *RoleHandler { return &RoleHandler{service: service} } // AdminGetAllRoles godoc // @Summary Get all roles (Admin) // @Description Get list of all roles with permissions // @Tags admin,roles // @Accept json // @Produce json // @Security BearerAuth // @Success 200 {array} models.Role // @Router /api/v1/admin/roles [get] func (h *RoleHandler) AdminGetAllRoles(c *gin.Context) { roles, err := h.service.GetAllRoles() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": roles}) } // AdminGetRoleByID godoc // @Summary Get role by ID (Admin) // @Description Get a single role by ID // @Tags admin,roles // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Success 200 {object} models.Role // @Router /api/v1/admin/roles/{id} [get] func (h *RoleHandler) AdminGetRoleByID(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"}) return } role, err := h.service.GetRoleByID(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } if role == nil { c.JSON(http.StatusNotFound, gin.H{"error": "Role not found"}) return } c.JSON(http.StatusOK, gin.H{"data": role}) } // AdminCreateRole godoc // @Summary Create a new role (Admin) // @Description Create a new role // @Tags admin,roles // @Accept json // @Produce json // @Security BearerAuth // @Param role body models.Role true "Role object" // @Success 201 {object} models.Role // @Router /api/v1/admin/roles [post] func (h *RoleHandler) AdminCreateRole(c *gin.Context) { var input struct { Name string `json:"name" binding:"required"` Description string `json:"description"` } if err := c.ShouldBindJSON(&input); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } role := &models.Role{ Name: input.Name, Description: input.Description, } if err := h.service.CreateRole(role); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusCreated, gin.H{"data": role}) } // AdminUpdateRole godoc // @Summary Update a role (Admin) // @Description Update an existing role // @Tags admin,roles // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Param role body models.Role true "Role object" // @Success 200 {object} models.Role // @Router /api/v1/admin/roles/{id} [put] func (h *RoleHandler) AdminUpdateRole(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"}) return } var input map[string]interface{} if err := c.ShouldBindJSON(&input); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) return } if err := h.service.UpdateRole(id, input); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } // Fetch updated role role, err := h.service.GetRoleByID(id) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"data": role}) } // AdminDeleteRole godoc // @Summary Delete a role (Admin) // @Description Delete a role by ID // @Tags admin,roles // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Success 200 {object} map[string]string // @Router /api/v1/admin/roles/{id} [delete] func (h *RoleHandler) AdminDeleteRole(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 64) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid role ID"}) return } if err := h.service.DeleteRole(id); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"message": "Role deleted successfully"}) }