ch some
CI/CD / Vet, test, build (push) Has been cancelled
CI/CD / Docker image (push) Has been cancelled
CI/CD / Deploy binary to server (push) Has been cancelled

This commit is contained in:
guwandyk committed 2026-09-04 19:33:26 +05:00
1 parent 28509eb4ca
commit d1a6fcaadc
102 files changed
+2089 -1957

No files matched your search

+62 -20
View File
@@ -8,6 +8,7 @@ import (
"online-back/internal/appresult"
"online-back/internal/handlers"
"online-back/internal/shared"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
@@ -28,6 +29,7 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
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)
@@ -41,6 +43,8 @@ func (h *handler) Register(router *gin.RouterGroup) {
// @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
@@ -64,6 +68,32 @@ func (h *handler) list(c *gin.Context) {
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
@@ -77,7 +107,7 @@ func (h *handler) list(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /permissions/{id} [get]
func (h *handler) getByID(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -115,6 +145,10 @@ func (h *handler) create(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if !IsKnownModule(dto.Module) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
item, err := h.repository.Create(c.Request.Context(), dto)
if err != nil {
@@ -141,7 +175,7 @@ func (h *handler) create(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /permissions/{id} [put]
func (h *handler) update(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -152,6 +186,10 @@ func (h *handler) update(c *gin.Context) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
if !IsKnownModule(dto.Module) {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
item, err := h.repository.Update(c.Request.Context(), id, dto)
if errors.Is(err, pgx.ErrNoRows) {
@@ -180,7 +218,7 @@ func (h *handler) update(c *gin.Context) {
// @Failure 500 {object} appresult.AppError
// @Router /permissions/{id} [delete]
func (h *handler) delete(c *gin.Context) {
id, ok := parseID(c.Param("id"))
id, ok := shared.ParseID(c.Param("id"))
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
@@ -198,24 +236,10 @@ func (h *handler) delete(c *gin.Context) {
c.JSON(http.StatusOK, appresult.SuccessWithData("permission removed"))
}
func parseID(raw string) (int64, bool) {
id, err := strconv.ParseInt(raw, 10, 64)
if err != nil || id <= 0 {
return 0, false
}
return id, true
}
const (
defaultPage = 1
defaultLimit = 10
maxLimit = 100
)
func parseListFilter(c *gin.Context) (ListFilter, bool) {
filter := ListFilter{
Page: defaultPage,
Limit: defaultLimit,
Page: shared.DefaultPage,
Limit: shared.DefaultLimit,
}
if raw := c.Query("page"); raw != "" {
@@ -228,7 +252,7 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
if raw := c.Query("limit"); raw != "" {
limit, err := strconv.Atoi(raw)
if err != nil || limit < 1 || limit > maxLimit {
if err != nil || limit < 1 || limit > shared.MaxLimit {
return ListFilter{}, false
}
filter.Limit = limit
@@ -237,6 +261,24 @@ func parseListFilter(c *gin.Context) (ListFilter, bool) {
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 ""
}