2025-07-21 22:38:02 +00:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"x-ui/database/model"
|
2025-07-21 23:54:53 +00:00
|
|
|
"x-ui/database"
|
2025-07-21 22:38:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type BlockedDomainController struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBlockedDomainController(g *gin.RouterGroup) *BlockedDomainController {
|
2025-07-21 23:54:53 +00:00
|
|
|
ctrl := &BlockedDomainController{}
|
2025-07-21 22:38:02 +00:00
|
|
|
r := g.Group("/blocked-domains")
|
|
|
|
r.GET("/", ctrl.List)
|
|
|
|
r.POST("/", ctrl.Create)
|
|
|
|
r.PUT("/:id", ctrl.Update)
|
|
|
|
r.DELETE("/:id", ctrl.Delete)
|
|
|
|
return ctrl
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctrl *BlockedDomainController) List(c *gin.Context) {
|
2025-07-21 23:54:53 +00:00
|
|
|
var domains []model.BlockedDomain
|
|
|
|
err := database.GetDB().Find(&domains).Error
|
2025-07-21 22:38:02 +00:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "obj": domains})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctrl *BlockedDomainController) Create(c *gin.Context) {
|
|
|
|
var domain model.BlockedDomain
|
2025-07-22 11:01:08 +00:00
|
|
|
if err := c.ShouldBind(&domain); err != nil {
|
2025-07-21 22:38:02 +00:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2025-07-21 23:54:53 +00:00
|
|
|
err := database.GetDB().Create(&domain).Error
|
|
|
|
if err != nil {
|
2025-07-21 22:38:02 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "obj": domain})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctrl *BlockedDomainController) Update(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "msg": "invalid id"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var domain model.BlockedDomain
|
2025-07-22 11:01:08 +00:00
|
|
|
if err := c.ShouldBind(&domain); err != nil {
|
2025-07-21 22:38:02 +00:00
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
domain.Id = id
|
2025-07-21 23:54:53 +00:00
|
|
|
err = database.GetDB().Save(&domain).Error
|
|
|
|
if err != nil {
|
2025-07-21 22:38:02 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true, "obj": domain})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ctrl *BlockedDomainController) Delete(c *gin.Context) {
|
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"success": false, "msg": "invalid id"})
|
|
|
|
return
|
|
|
|
}
|
2025-07-21 23:54:53 +00:00
|
|
|
err = database.GetDB().Delete(&model.BlockedDomain{}, id).Error
|
|
|
|
if err != nil {
|
2025-07-21 22:38:02 +00:00
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"success": false, "msg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"success": true})
|
|
|
|
}
|