277 lines
7.9 KiB
Go
277 lines
7.9 KiB
Go
package permission
|
|
|
|
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 = "/permissions"
|
|
|
|
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("/tree", h.tree)
|
|
group.GET("/:id", h.getByID)
|
|
group.POST("", h.create)
|
|
group.PUT("/:id", h.update)
|
|
group.DELETE("/:id", h.delete)
|
|
}
|
|
|
|
// list godoc
|
|
// @Summary List permissions
|
|
// @Description Returns paginated permissions with optional filters
|
|
// @Tags permission
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param permission_name query string false "Filter by name"
|
|
// @Param guard_name query string false "Filter by guard (web, operator, admin)"
|
|
// @Param module query string false "Filter by saha (cards, loans, …)"
|
|
// @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 /permissions [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 permissions failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// tree godoc
|
|
// @Summary Permission tree
|
|
// @Description Returns permissions grouped as guard → module (saha) → permissions
|
|
// @Tags permission
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param guard_name query string false "Filter by guard (web, operator, admin)"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permissions/tree [get]
|
|
func (h *handler) tree(c *gin.Context) {
|
|
guardName := strings.TrimSpace(c.Query("guard_name"))
|
|
if guardName == "" {
|
|
guardName = strings.TrimSpace(c.Query("guardName"))
|
|
}
|
|
|
|
result, err := h.repository.Tree(c.Request.Context(), guardName)
|
|
if err != nil {
|
|
h.logger.Errorf("permission tree failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
|
}
|
|
|
|
// getByID godoc
|
|
// @Summary Get permission by ID
|
|
// @Description Returns a single permission
|
|
// @Tags permission
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Permission ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permissions/{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 permission failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// create godoc
|
|
// @Summary Create permission
|
|
// @Description Creates a new permission
|
|
// @Tags permission
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param body body CreatePermissionDTO true "Permission payload"
|
|
// @Success 201 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permissions [post]
|
|
func (h *handler) create(c *gin.Context) {
|
|
var dto CreatePermissionDTO
|
|
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 permission failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// update godoc
|
|
// @Summary Update permission
|
|
// @Description Updates an existing permission
|
|
// @Tags permission
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Permission ID"
|
|
// @Param body body UpdatePermissionDTO true "Permission payload"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permissions/{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 UpdatePermissionDTO
|
|
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 permission failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
|
}
|
|
|
|
// delete godoc
|
|
// @Summary Delete permission
|
|
// @Description Deletes a permission
|
|
// @Tags permission
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param id path int true "Permission ID"
|
|
// @Success 200 {object} appresult.AppSuccess
|
|
// @Failure 400 {object} appresult.AppError
|
|
// @Failure 404 {object} appresult.AppError
|
|
// @Failure 500 {object} appresult.AppError
|
|
// @Router /permissions/{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 permission failed: %v", err)
|
|
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, appresult.SuccessWithData("permission 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("permission_name")); raw != "" {
|
|
filter.PermissionName = raw
|
|
}
|
|
if raw := firstQuery(c, "guard_name", "guardName"); raw != "" {
|
|
filter.GuardName = raw
|
|
}
|
|
if raw := firstQuery(c, "module"); raw != "" {
|
|
filter.Module = raw
|
|
}
|
|
|
|
return filter, true
|
|
}
|
|
|
|
func firstQuery(c *gin.Context, keys ...string) string {
|
|
for _, key := range keys {
|
|
if key == "" {
|
|
continue
|
|
}
|
|
if raw := strings.TrimSpace(c.Query(key)); raw != "" {
|
|
return raw
|
|
}
|
|
}
|
|
return ""
|
|
}
|