93 lines
2.2 KiB
Go
93 lines
2.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"filestoragesystem/internal/middleware"
|
|
"filestoragesystem/internal/utils"
|
|
)
|
|
|
|
// WebhookController Webhook控制器
|
|
type WebhookController struct {
|
|
*Base
|
|
}
|
|
|
|
// NewWebhookController 创建Webhook控制器
|
|
func NewWebhookController(b *Base) *WebhookController { return &WebhookController{Base: b} }
|
|
|
|
type createWebhookReq struct {
|
|
URL string `json:"url" binding:"required"`
|
|
ProjectID *uint `json:"project_id"`
|
|
Secret string `json:"secret"`
|
|
Events string `json:"events" binding:"required"` // 如 file.upload,file.delete
|
|
Status int8 `json:"status"`
|
|
}
|
|
|
|
// Create 创建webhook
|
|
func (ctl *WebhookController) Create(c *gin.Context) {
|
|
var req createWebhookReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
userID := middleware.CurrentUserID(c)
|
|
w, err := ctl.svc.Webhook.Create(userID, req.ProjectID, req.URL, req.Secret, req.Events, req.Status)
|
|
if err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "create", "webhook", w.ID, w.URL, true, "")
|
|
utils.OK(c, w)
|
|
}
|
|
|
|
// List webhook列表
|
|
func (ctl *WebhookController) List(c *gin.Context) {
|
|
userID := middleware.CurrentUserID(c)
|
|
hooks, err := ctl.svc.Webhook.List(userID)
|
|
if err != nil {
|
|
utils.ServerError(c, err.Error())
|
|
return
|
|
}
|
|
utils.OK(c, hooks)
|
|
}
|
|
|
|
type updateWebhookReq struct {
|
|
URL string `json:"url"`
|
|
Events string `json:"events"`
|
|
Status int8 `json:"status"`
|
|
}
|
|
|
|
// Update 更新webhook
|
|
func (ctl *WebhookController) Update(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
var req updateWebhookReq
|
|
if !bindJSON(c, &req) {
|
|
return
|
|
}
|
|
userID := middleware.CurrentUserID(c)
|
|
w, err := ctl.svc.Webhook.Update(userID, id, req.URL, req.Events, req.Status)
|
|
if err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "update", "webhook", id, w.URL, true, "")
|
|
utils.OK(c, w)
|
|
}
|
|
|
|
// Delete 删除webhook
|
|
func (ctl *WebhookController) Delete(c *gin.Context) {
|
|
id, ok := paramID(c)
|
|
if !ok {
|
|
return
|
|
}
|
|
userID := middleware.CurrentUserID(c)
|
|
if err := ctl.svc.Webhook.Delete(userID, id); err != nil {
|
|
utils.FailMsg(c, err.Error())
|
|
return
|
|
}
|
|
ctl.svc.RecordOpLog(ctl.opCtx(c), "delete", "webhook", id, "", true, "")
|
|
utils.OKMsg(c, "webhook已删除")
|
|
}
|