Quellcode durchsuchen

refactor(cc): 重构 CC列表接口和数据结构

- 新增 CCList 结构体,用于存储 CC 列表数据
fusu vor 3 Wochen
Ursprung
Commit
64f291b880
3 geänderte Dateien mit 36 neuen und 12 gelöschten Zeilen
  1. 17 6
      api/v1/cc.go
  2. 5 5
      internal/repository/cc.go
  3. 14 1
      internal/service/cc.go

+ 17 - 6
api/v1/cc.go

@@ -7,13 +7,24 @@ type CCListRequest struct {
 }
 
 
+type CCList struct {
+	Value string `json:"value" form:"value" gorm:"column:value"`
+	Type string `json:"type" form:"type" gorm:"column:type"`
+	Reason string `json:"reason" form:"reason" gorm:"column:reason"`
+	SourceURL string `json:"sourceURL" form:"sourceURL" gorm:"column:sourceURL"`
+	SourceUserAgent string `json:"sourceUserAgent" form:"sourceUserAgent" gorm:"column:sourceUserAgent"`
+	CreatedAt int64 `json:"createdAt" form:"createdAt" gorm:"column:createdAt"`
+	ExpiredAt int64 `json:"expiredAt" form:"expiredAt" gorm:"column:expiredAt"`
+}
+
 type CCListResponse struct {
-	Value string `json:"value" form:"value" gorm:"value"`
-	Type string `json:"type" form:"type" gorm:"type"`
-	Reason string `json:"reason" form:"reason" gorm:"reason"`
-	SourceURL string `json:"sourceURL" form:"sourceURL" gorm:"sourceURL"`
-	CreatedAt int64 `json:"createdAt" form:"createdAt" gorm:"createdAt"`
-	ExpiredAt int64 `json:"expiredAt" form:"expiredAt" gorm:"expiredAt"`
+	Value string `json:"value" form:"value"`
+	Type string `json:"type" form:"type"`
+	Reason string `json:"reason" form:"reason"`
+	SourceURL string `json:"sourceURL" form:"sourceURL"`
+	SourceUserAgent string `json:"sourceUserAgent" form:"sourceUserAgent"`
+	CreatedAt string `json:"createdAt" form:"createdAt"`
+	ExpiredAt string `json:"expiredAt" form:"expiredAt"`
 }
 
 type CCStateRequest struct {

+ 5 - 5
internal/repository/cc.go

@@ -6,7 +6,7 @@ import (
 )
 
 type CcRepository interface {
-	GetCcList(ctx context.Context, serviceId int64) ([]v1.CCListResponse, error)
+	GetCcList(ctx context.Context, serviceId int64) ([]v1.CCList, error)
 	EditCcState(ctx context.Context, serviceId int64, ip string) error
 }
 
@@ -22,16 +22,16 @@ type ccRepository struct {
 	*Repository
 }
 
-func (r *ccRepository) GetCcList(ctx context.Context, serviceId int64) ([]v1.CCListResponse, error) {
-	var req []v1.CCListResponse
-	if err := r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("serverId = ? AND state = 1", serviceId).Select("value,type,reason,sourceURL,createdAt,expiredAt").Scan(&req).Error; err != nil {
+func (r *ccRepository) GetCcList(ctx context.Context, serviceId int64) ([]v1.CCList, error) {
+	var req []v1.CCList
+	if err := r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("serverId = ? AND state = 1", serviceId).Select("value,type,reason,sourceURL,sourceUserAgent,createdAt,expiredAt").Scan(&req).Error; err != nil {
 		return nil, err
 	}
 	return req, nil
 }
 
 func (r *ccRepository) EditCcState(ctx context.Context, serviceId int64, ip string) error {
-	if err := r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("serverId = ? AND ipFrom = ?", serviceId, ip).Update("state", 0).Error; err != nil {
+	if err := r.DBWithName(ctx,"cdn").Table("cloud_ip_items").Where("serverId = ? AND value = ?", serviceId, ip).Update("state", 0).Error; err != nil {
 		return err
 	}
 	return nil

+ 14 - 1
internal/service/cc.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	v1 "github.com/go-nunu/nunu-layout-advanced/api/v1"
 	"github.com/go-nunu/nunu-layout-advanced/internal/repository"
+	"time"
 )
 
 type CcService interface {
@@ -41,7 +42,19 @@ func (s *ccService) GetCcList(ctx context.Context, req v1.CCListRequest) ([]v1.C
 	if err != nil {
 		return nil, err
 	}
-	return ccList, nil
+	var res []v1.CCListResponse
+	for _, v := range ccList {
+		res = append(res, v1.CCListResponse{
+			Value: v.Value,
+			Type: v.Type,
+			Reason: v.Reason,
+			SourceURL: v.SourceURL,
+			SourceUserAgent: v.SourceUserAgent,
+			CreatedAt: time.Unix(v.CreatedAt, 0).Format("2006-01-02 15:04:05"),
+			ExpiredAt: time.Unix(v.ExpiredAt, 0).Format("2006-01-02 15:04:05"),
+		})
+	}
+	return res, nil
 }
 
 func (s *ccService) EditCcState(ctx context.Context, req []v1.CCStateRequest) error {