mirror of
https://github.com/APIParkLab/APIPark.git
synced 2026-06-04 10:13:53 +08:00
Add filtering options to the list
This commit is contained in:
@@ -13,7 +13,7 @@ type IKeyController interface {
|
||||
Edit(ctx *gin.Context, providerId string, id string, input *ai_key_dto.Edit) error
|
||||
Delete(ctx *gin.Context, providerId string, id string) error
|
||||
Get(ctx *gin.Context, providerId string, id string) (*ai_key_dto.Key, error)
|
||||
List(ctx *gin.Context, providerId string, keyword string, page string, pageSize string) ([]*ai_key_dto.Item, int64, error)
|
||||
List(ctx *gin.Context, providerId string, keyword string, page string, pageSize string, statuses string) ([]*ai_key_dto.Item, int64, error)
|
||||
Enable(ctx *gin.Context, providerId string, id string) error
|
||||
Disable(ctx *gin.Context, providerId string, id string) error
|
||||
Sort(ctx *gin.Context, providerId string, input *ai_key_dto.Sort) error
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package ai_key
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
ai_key "github.com/APIParkLab/APIPark/module/ai-key"
|
||||
@@ -38,7 +39,7 @@ func (i *imlAIKeyController) Get(ctx *gin.Context, providerId string, id string)
|
||||
return i.module.Get(ctx, providerId, id)
|
||||
}
|
||||
|
||||
func (i *imlAIKeyController) List(ctx *gin.Context, providerId string, keyword string, page string, pageSize string) ([]*ai_key_dto.Item, int64, error) {
|
||||
func (i *imlAIKeyController) List(ctx *gin.Context, providerId string, keyword string, page string, pageSize string, statuses string) ([]*ai_key_dto.Item, int64, error) {
|
||||
p, err := strconv.Atoi(page)
|
||||
if err != nil {
|
||||
if page != "" {
|
||||
@@ -51,9 +52,16 @@ func (i *imlAIKeyController) List(ctx *gin.Context, providerId string, keyword s
|
||||
if pageSize != "" {
|
||||
return nil, 0, err
|
||||
}
|
||||
ps = 15
|
||||
ps = 20
|
||||
}
|
||||
return i.module.List(ctx, providerId, keyword, p, ps)
|
||||
ss := make([]string, 0)
|
||||
if statuses != "" {
|
||||
err = json.Unmarshal([]byte(statuses), &ss)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
}
|
||||
return i.module.List(ctx, providerId, keyword, p, ps, ss)
|
||||
}
|
||||
|
||||
func (i *imlAIKeyController) Sort(ctx *gin.Context, providerId string, input *ai_key_dto.Sort) error {
|
||||
|
||||
+44
-5
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
+1
-1
@@ -27,7 +27,7 @@ func (p *plugin) aiAPIs() []pm3.Api {
|
||||
func (p *plugin) aiKeyApis() []pm3.Api {
|
||||
return []pm3.Api{
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/resource/key", []string{"context", "query:provider", "query:id"}, []string{"info"}, p.aiKeyController.Get),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/resource/keys", []string{"context", "query:provider", "query:keyword", "query:page", "query:page_size"}, []string{"keys", "total"}, p.aiKeyController.List),
|
||||
pm3.CreateApiWidthDoc(http.MethodGet, "/api/v1/ai/resource/keys", []string{"context", "query:provider", "query:keyword", "query:page", "query:page_size", "query:statuses"}, []string{"keys", "total"}, p.aiKeyController.List),
|
||||
pm3.CreateApiWidthDoc(http.MethodPost, "/api/v1/ai/resource/key", []string{"context", "query:provider", "body"}, nil, p.aiKeyController.Create),
|
||||
pm3.CreateApiWidthDoc(http.MethodPut, "/api/v1/ai/resource/key", []string{"context", "query:provider", "query:id", "body"}, nil, p.aiKeyController.Edit),
|
||||
pm3.CreateApiWidthDoc(http.MethodDelete, "/api/v1/ai/resource/key", []string{"context", "query:provider", "query:id"}, nil, p.aiKeyController.Delete),
|
||||
|
||||
@@ -23,6 +23,29 @@ type imlAIKeyService struct {
|
||||
universally.IServiceDelete
|
||||
}
|
||||
|
||||
func (i *imlAIKeyService) SearchUnExpiredByPage(ctx context.Context, w map[string]interface{}, page, pageSize int, order string) ([]*Key, int64, error) {
|
||||
sql := "(expire_time = 0 || expire_time > ?)"
|
||||
args := []interface{}{time.Now().Unix()}
|
||||
for k, v := range w {
|
||||
switch v.(type) {
|
||||
case []int:
|
||||
sql += fmt.Sprintf(" and `%s` in (?)", k)
|
||||
default:
|
||||
sql += fmt.Sprintf(" and `%s` = ?", k)
|
||||
}
|
||||
args = append(args, v)
|
||||
}
|
||||
list, total, err := i.store.ListPage(ctx, sql, page, pageSize, args, order)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var result []*Key
|
||||
for _, item := range list {
|
||||
result = append(result, FromEntity(item))
|
||||
}
|
||||
return result, total, nil
|
||||
}
|
||||
|
||||
func (i *imlAIKeyService) KeysAfterPriority(ctx context.Context, providerId string, priority int) ([]*Key, error) {
|
||||
list, err := i.store.ListQuery(ctx, "sort > ? and provider = ?", []interface{}{priority, providerId}, "sort asc")
|
||||
if err != nil {
|
||||
|
||||
@@ -20,6 +20,7 @@ type IKeyService interface {
|
||||
SortBefore(ctx context.Context, provider string, originID string, targetID string) ([]*Key, error)
|
||||
SortAfter(ctx context.Context, provider string, originID string, targetID string) ([]*Key, error)
|
||||
KeysAfterPriority(ctx context.Context, providerId string, priority int) ([]*Key, error)
|
||||
SearchUnExpiredByPage(ctx context.Context, w map[string]interface{}, page, pageSize int, order string) ([]*Key, int64, error)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user