Browse Source

fix(api/v1): 优化 IP 黑白名单处理逻辑

- 在 AllowAndDenyIpRequest 结构中,将 allowOrDeny 字段的验证规则改为默认值 0
- 在 EditAllowAndDenyIps 方法中,使用 map 更新 allow_or_deny 字段,避免其他字段被意外修改
fusu 4 weeks ago
parent
commit
8417e52809
2 changed files with 5 additions and 2 deletions
  1. 1 1
      api/v1/allowAndDenyIp.go
  2. 4 1
      internal/repository/allowanddenyip.go

+ 1 - 1
api/v1/allowAndDenyIp.go

@@ -3,7 +3,7 @@ package v1
 type AllowAndDenyIpRequest struct {
 	Id     int    `json:"id" form:"id"`
 	Ip     string `json:"ip" form:"ip" validate:"required"`
-	AllowOrDeny int `json:"allowOrDeny" form:"allowOrDeny" validate:"required"`
+	AllowOrDeny int `json:"allowOrDeny" form:"allowOrDeny" default:"0"`
 	HostId int `json:"hostId" form:"hostId" validate:"required"`
 	Uid    int `json:"uid" form:"uid" validate:"required"`
 }

+ 4 - 1
internal/repository/allowanddenyip.go

@@ -42,7 +42,10 @@ func (r *allowAndDenyIpRepository) AddAllowAndDenyIps(ctx context.Context, req m
 }
 
 func (r *allowAndDenyIpRepository) EditAllowAndDenyIps(ctx context.Context, req model.AllowAndDenyIp) error {
-	if err := r.db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Error; err != nil {
+	allowAndDenyIp := map[string]interface{}{
+		"allow_or_deny": req.AllowOrDeny,
+	}
+	if err := r.db.WithContext(ctx).Where("id = ?", req.Id).Updates(&req).Updates(allowAndDenyIp).Error; err != nil {
 		return fmt.Errorf("update error: %v", err)
 	}
 	return nil