Create 创建新的通知渠道。
(c *gin.Context)
| 42 | |
| 43 | // Create 创建新的通知渠道。 |
| 44 | func (h *NotifyHandler) Create(c *gin.Context) { |
| 45 | var req notifyCreateRequest |
| 46 | if err := c.ShouldBindJSON(&req); err != nil { |
| 47 | Error(c, http.StatusBadRequest, ErrInvalidParams, err.Error()) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | ctx := c.Request.Context() |
| 52 | |
| 53 | // 验证配置 |
| 54 | if err := h.svc.Notify.ValidateChannelConfig(req.Type, req.Config); err != nil { |
| 55 | Error(c, http.StatusBadRequest, ErrInvalidParams, "配置验证失败: "+err.Error()) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // 加密配置 |
| 60 | configJSON, _ := json.Marshal(req.Config) |
| 61 | configStr := string(configJSON) |
| 62 | if h.svc.Crypto != nil { |
| 63 | configStr = h.svc.Crypto.Encrypt(configStr) |
| 64 | } |
| 65 | |
| 66 | // 序列化事件列表 |
| 67 | eventsJSON, _ := json.Marshal(req.Events) |
| 68 | eventsStr := string(eventsJSON) |
| 69 | |
| 70 | channel := &model.NotifyChannel{ |
| 71 | Name: req.Name, |
| 72 | Type: req.Type, |
| 73 | Enabled: req.Enabled, |
| 74 | Config: configStr, |
| 75 | Events: eventsStr, |
| 76 | } |
| 77 | |
| 78 | if err := h.svc.Repo.NotifyChannel.Create(ctx, channel); err != nil { |
| 79 | Error(c, http.StatusInternalServerError, ErrInternal, "创建失败: "+err.Error()) |
| 80 | return |
| 81 | } |
| 82 | |
| 83 | Success(c, channel) |
| 84 | } |
| 85 | |
| 86 | // List 返回所有通知渠道。 |
| 87 | func (h *NotifyHandler) List(c *gin.Context) { |