mirror of
https://github.com/Awuqing/BackupX.git
synced 2026-05-10 17:43:43 +08:00
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package http
|
|
|
|
import (
|
|
"backupx/server/internal/apperror"
|
|
"backupx/server/internal/service"
|
|
"backupx/server/pkg/response"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type NotificationHandler struct {
|
|
service *service.NotificationService
|
|
}
|
|
|
|
func NewNotificationHandler(notificationService *service.NotificationService) *NotificationHandler {
|
|
return &NotificationHandler{service: notificationService}
|
|
}
|
|
|
|
func (h *NotificationHandler) List(c *gin.Context) {
|
|
items, err := h.service.List(c.Request.Context())
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, items)
|
|
}
|
|
|
|
func (h *NotificationHandler) Get(c *gin.Context) {
|
|
id, ok := parseUintParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
item, err := h.service.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, item)
|
|
}
|
|
|
|
func (h *NotificationHandler) Create(c *gin.Context) {
|
|
var input service.NotificationUpsertInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
response.Error(c, apperror.BadRequest("NOTIFICATION_INVALID", "通知配置参数不合法", err))
|
|
return
|
|
}
|
|
item, err := h.service.Create(c.Request.Context(), input)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, item)
|
|
}
|
|
|
|
func (h *NotificationHandler) Update(c *gin.Context) {
|
|
id, ok := parseUintParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
var input service.NotificationUpsertInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
response.Error(c, apperror.BadRequest("NOTIFICATION_INVALID", "通知配置参数不合法", err))
|
|
return
|
|
}
|
|
item, err := h.service.Update(c.Request.Context(), id, input)
|
|
if err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, item)
|
|
}
|
|
|
|
func (h *NotificationHandler) Delete(c *gin.Context) {
|
|
id, ok := parseUintParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.service.Delete(c.Request.Context(), id); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"deleted": true})
|
|
}
|
|
|
|
func (h *NotificationHandler) Test(c *gin.Context) {
|
|
var input service.NotificationUpsertInput
|
|
if err := c.ShouldBindJSON(&input); err != nil {
|
|
response.Error(c, apperror.BadRequest("NOTIFICATION_INVALID", "通知配置参数不合法", err))
|
|
return
|
|
}
|
|
if err := h.service.Test(c.Request.Context(), input); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"success": true})
|
|
}
|
|
|
|
func (h *NotificationHandler) TestSaved(c *gin.Context) {
|
|
id, ok := parseUintParam(c, "id")
|
|
if !ok {
|
|
return
|
|
}
|
|
if err := h.service.TestSaved(c.Request.Context(), id); err != nil {
|
|
response.Error(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"success": true})
|
|
}
|