package role import ( "errors" "net/http" "strconv" "strings" "online-back/internal/appresult" "online-back/internal/handlers" "online-back/internal/shared" "online-back/pkg/logging" "github.com/gin-gonic/gin" "github.com/jackc/pgx/v4" ) const baseURL = "/roles" type handler struct { logger *logging.Logger repository Repository } func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler { return &handler{logger: logger, repository: repository} } func (h *handler) Register(router *gin.RouterGroup) { group := router.Group(baseURL, appresult.JwtTokenCheck()) group.GET("", h.list) group.GET("/:id", h.getByID) group.POST("", h.create) group.PUT("/:id", h.update) group.DELETE("/:id", h.delete) } // list godoc // @Summary List roles // @Description Returns paginated roles with optional filters // @Tags role // @Produce json // @Security BearerAuth // @Param role_name query string false "Filter by name" // @Param page query int false "Page number" default(1) // @Param limit query int false "Items per page" default(10) // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /roles [get] func (h *handler) list(c *gin.Context) { filter, ok := parseListFilter(c) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } result, err := h.repository.List(c.Request.Context(), filter) if err != nil { h.logger.Errorf("list roles failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(result)) } // getByID godoc // @Summary Get role by ID // @Description Returns a single role // @Tags role // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /roles/{id} [get] func (h *handler) getByID(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.GetByID(c.Request.Context(), id) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("get role failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // create godoc // @Summary Create role // @Description Creates a new role // @Tags role // @Accept json // @Produce json // @Security BearerAuth // @Param body body CreateRoleDTO true "Role payload" // @Success 201 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /roles [post] func (h *handler) create(c *gin.Context) { var dto CreateRoleDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Create(c.Request.Context(), dto) if err != nil { h.logger.Errorf("create role failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusCreated, appresult.SuccessWithData(item)) } // update godoc // @Summary Update role // @Description Updates an existing role // @Tags role // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Param body body UpdateRoleDTO true "Role payload" // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /roles/{id} [put] func (h *handler) update(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } var dto UpdateRoleDTO if err := c.ShouldBindJSON(&dto); err != nil { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } item, err := h.repository.Update(c.Request.Context(), id, dto) if errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } if err != nil { h.logger.Errorf("update role failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData(item)) } // delete godoc // @Summary Delete role // @Description Deletes a role // @Tags role // @Produce json // @Security BearerAuth // @Param id path int true "Role ID" // @Success 200 {object} appresult.AppSuccess // @Failure 400 {object} appresult.AppError // @Failure 404 {object} appresult.AppError // @Failure 500 {object} appresult.AppError // @Router /roles/{id} [delete] func (h *handler) delete(c *gin.Context) { id, ok := shared.ParseID(c.Param("id")) if !ok { c.JSON(http.StatusBadRequest, appresult.ErrMissingParam) return } if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) { c.JSON(http.StatusNotFound, appresult.ErrNotFound) return } else if err != nil { h.logger.Errorf("delete role failed: %v", err) c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer) return } c.JSON(http.StatusOK, appresult.SuccessWithData("role removed")) } func parseListFilter(c *gin.Context) (ListFilter, bool) { filter := ListFilter{ Page: shared.DefaultPage, Limit: shared.DefaultLimit, } if raw := c.Query("page"); raw != "" { page, err := strconv.Atoi(raw) if err != nil || page < 1 { return ListFilter{}, false } filter.Page = page } if raw := c.Query("limit"); raw != "" { limit, err := strconv.Atoi(raw) if err != nil || limit < 1 || limit > shared.MaxLimit { return ListFilter{}, false } filter.Limit = limit } if raw := strings.TrimSpace(c.Query("role_name")); raw != "" { filter.RoleName = raw } return filter, true }