first commit
This commit is contained in:
70
app/account/handlers/permission_handler.go
Normal file
70
app/account/handlers/permission_handler.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"gobeyhan/app/account/services"
|
||||
"gobeyhan/database/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type PermissionHandler struct {
|
||||
service *services.PermissionService
|
||||
}
|
||||
|
||||
func NewPermissionHandler(service *services.PermissionService) *PermissionHandler {
|
||||
return &PermissionHandler{service: service}
|
||||
}
|
||||
|
||||
// AdminGetAllPermissions godoc
|
||||
// @Summary Get all permissions (Admin)
|
||||
// @Description Get list of all permissions
|
||||
// @Tags admin,permissions
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {array} models.Permission
|
||||
// @Router /api/v1/admin/permissions [get]
|
||||
func (h *PermissionHandler) AdminGetAllPermissions(c *gin.Context) {
|
||||
permissions, err := h.service.GetAllPermissions()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"data": permissions})
|
||||
}
|
||||
|
||||
// AdminCreatePermission godoc
|
||||
// @Summary Create a new permission (Admin)
|
||||
// @Description Create a new permission
|
||||
// @Tags admin,permissions
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param permission body models.Permission true "Permission object"
|
||||
// @Success 201 {object} models.Permission
|
||||
// @Router /api/v1/admin/permissions [post]
|
||||
func (h *PermissionHandler) AdminCreatePermission(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
|
||||
}
|
||||
|
||||
permission := &models.Permission{
|
||||
Name: input.Name,
|
||||
Description: input.Description,
|
||||
}
|
||||
|
||||
if err := h.service.CreatePermission(permission); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"data": permission})
|
||||
}
|
||||
Reference in New Issue
Block a user