90 lines
2.1 KiB
Go
90 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/go-kratos/kratos/v3/errors"
|
|
"kra/internal/service/business"
|
|
"kra/internal/service/dto"
|
|
"strconv"
|
|
)
|
|
|
|
type AlertRule struct{ service *business.AlertRuleService }
|
|
|
|
func NewAlertRule(service *business.AlertRuleService) *AlertRule { return &AlertRule{service: service} }
|
|
func (h *AlertRule) List(c *gin.Context) {
|
|
var req dto.AlertRuleQuery
|
|
if err := c.ShouldBindQuery(&req); err != nil {
|
|
Fail(c, "查询参数不正确")
|
|
return
|
|
}
|
|
v, err := h.service.List(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, v)
|
|
}
|
|
func (h *AlertRule) Types(c *gin.Context) {
|
|
v, err := h.service.Types(c.Request.Context())
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, v)
|
|
}
|
|
func (h *AlertRule) Find(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
Fail(c, "规则 ID 不正确")
|
|
return
|
|
}
|
|
v, err := h.service.Find(c.Request.Context(), id)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, v)
|
|
}
|
|
func (h *AlertRule) Save(c *gin.Context) {
|
|
var req dto.AlertRuleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
Fail(c, "预警规则参数不正确")
|
|
return
|
|
}
|
|
id, err := h.service.Save(c.Request.Context(), &req)
|
|
if err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OKWithData(c, gin.H{"id": id})
|
|
}
|
|
func (h *AlertRule) Delete(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
Fail(c, "规则 ID 不正确")
|
|
return
|
|
}
|
|
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OK(c)
|
|
}
|
|
func (h *AlertRule) Toggle(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
Fail(c, "规则 ID 不正确")
|
|
return
|
|
}
|
|
enabled, err := strconv.Atoi(c.Query("enabled"))
|
|
if err != nil {
|
|
Fail(c, "启用状态参数不正确")
|
|
return
|
|
}
|
|
if err := h.service.Toggle(c.Request.Context(), id, enabled); err != nil {
|
|
Fail(c, errors.FromError(err).Message)
|
|
return
|
|
}
|
|
OK(c)
|
|
}
|