Add filtering options to the list

This commit is contained in:
Liujian
2024-12-26 18:06:54 +08:00
parent 1bdd4720bb
commit 8a589982d1
7 changed files with 82 additions and 11 deletions
+44 -5
View File
@@ -6,6 +6,8 @@ import (
"fmt"
"time"
"github.com/eolinker/go-common/utils"
"gorm.io/gorm"
"github.com/eolinker/go-common/auto"
@@ -192,7 +194,7 @@ func (i *imlKeyModule) Get(ctx context.Context, providerId string, id string) (*
}, nil
}
func (i *imlKeyModule) List(ctx context.Context, providerId string, keyword string, page, pageSize int) ([]*ai_key_dto.Item, int64, error) {
func (i *imlKeyModule) List(ctx context.Context, providerId string, keyword string, page, pageSize int, statuses []string) ([]*ai_key_dto.Item, int64, error) {
_, err := i.aiKeyService.DefaultKey(ctx, providerId)
if err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
@@ -217,12 +219,49 @@ func (i *imlKeyModule) List(ctx context.Context, providerId string, keyword stri
return nil, 0, fmt.Errorf("create default key failed: %w", err)
}
}
list, total, err := i.aiKeyService.SearchByPage(ctx, keyword, map[string]interface{}{
w := map[string]interface{}{
"provider": providerId,
}, page, pageSize, "sort asc")
if err != nil {
return nil, 0, err
}
hasExpired := true
if len(statuses) > 0 {
hasExpired = false
statusConditions := make([]int, 0, len(statuses))
for _, s := range statuses {
status := ai_key_dto.KeyStatus(s)
if status == ai_key_dto.KeyExpired {
hasExpired = true
}
statusConditions = append(statusConditions, status.Int())
}
w["status"] = statusConditions
}
var list []*ai_key.Key
var total int64
if !hasExpired {
if keyword != "" {
list, err = i.aiKeyService.Search(ctx, keyword, w, "sort asc")
if err != nil {
return nil, 0, err
}
if len(list) == 0 {
return nil, 0, nil
}
uuids := utils.SliceToSlice(list, func(key *ai_key.Key) string {
return key.ID
})
w["uuid"] = uuids
}
list, total, err = i.aiKeyService.SearchUnExpiredByPage(ctx, w, page, pageSize, "sort asc")
if err != nil {
return nil, 0, err
}
} else {
list, total, err = i.aiKeyService.SearchByPage(ctx, keyword, w, page, pageSize, "sort asc")
if err != nil {
return nil, 0, err
}
}
var result []*ai_key_dto.Item
for _, item := range list {
status := ai_key_dto.ToKeyStatus(item.Status)
+1 -1
View File
@@ -13,7 +13,7 @@ type IKeyModule interface {
Edit(ctx context.Context, providerId string, id string, input *ai_key_dto.Edit) error
Delete(ctx context.Context, providerId string, id string) error
Get(ctx context.Context, providerId string, id string) (*ai_key_dto.Key, error)
List(ctx context.Context, providerId string, keyword string, page, pageSize int) ([]*ai_key_dto.Item, int64, error)
List(ctx context.Context, providerId string, keyword string, page, pageSize int, statuses []string) ([]*ai_key_dto.Item, int64, error)
UpdateKeyStatus(ctx context.Context, providerId string, id string, enable bool) error
Sort(ctx context.Context, providerId string, input *ai_key_dto.Sort) error
}