47 lines
1.4 KiB
Go
47 lines
1.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/middleware"
|
|
"github.com/yuxingu/digital-psychology/apps/api/internal/service/admin"
|
|
"github.com/yuxingu/digital-psychology/apps/api/pkg/response"
|
|
)
|
|
|
|
func (h *AdminHandler) registerBlockPolicies(authed *gin.RouterGroup) {
|
|
g := authed.Group("/content-safety")
|
|
g.GET("/block-policies", middleware.RequireAdminPermission(h.Svc, admin.PermContentSafetyRead), h.ListBlockPolicies)
|
|
g.GET("/block-policies/:id", middleware.RequireAdminPermission(h.Svc, admin.PermContentSafetyRead), h.GetBlockPolicy)
|
|
}
|
|
|
|
func (h *AdminHandler) ListBlockPolicies(c *gin.Context) {
|
|
items, err := h.Svc.ListBlockPolicies(c.Request.Context())
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50050, "list block-policy failed")
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"items": items})
|
|
}
|
|
|
|
func (h *AdminHandler) GetBlockPolicy(c *gin.Context) {
|
|
id, err := uuid.Parse(c.Param("id"))
|
|
if err != nil {
|
|
response.Fail(c, http.StatusBadRequest, 40002, "invalid id")
|
|
return
|
|
}
|
|
row, err := h.Svc.GetBlockPolicy(c.Request.Context(), id)
|
|
if errors.Is(err, admin.ErrBlockPolicyNotFound) {
|
|
response.Fail(c, http.StatusNotFound, 40420, "block-policy not found")
|
|
return
|
|
}
|
|
if err != nil {
|
|
response.Fail(c, http.StatusInternalServerError, 50051, "get block-policy failed")
|
|
return
|
|
}
|
|
response.OK(c, row)
|
|
}
|