feat(keys): sort by current concurrency

This commit is contained in:
Bestony@Homelab committed 2026-07-08 15:51:22 +08:00
1 parent 6f43986c37
commit 5debe1db33
6 files changed
+358 -22

No files matched your search

+35 -4
View File
@@ -388,10 +388,9 @@ func (r *apiKeyRepository) deleteWithAudit(ctx context.Context, exec *dbent.Clie
return nil
}
func (r *apiKeyRepository) ListByUserID(ctx context.Context, userID int64, params pagination.PaginationParams, filters service.APIKeyListFilters) ([]service.APIKey, *pagination.PaginationResult, error) {
func (r *apiKeyRepository) apiKeyListByUserIDQuery(userID int64, filters service.APIKeyListFilters) *dbent.APIKeyQuery {
q := r.activeQuery().Where(apikey.UserIDEQ(userID))
// Apply filters
if filters.Search != "" {
q = q.Where(apikey.Or(
apikey.NameContainsFold(filters.Search),
@@ -409,6 +408,12 @@ func (r *apiKeyRepository) ListByUserID(ctx context.Context, userID int64, param
}
}
return q
}
func (r *apiKeyRepository) ListByUserID(ctx context.Context, userID int64, params pagination.PaginationParams, filters service.APIKeyListFilters) ([]service.APIKey, *pagination.PaginationResult, error) {
q := r.apiKeyListByUserIDQuery(userID, filters)
total, err := q.Count(ctx)
if err != nil {
return nil, nil, err
@@ -435,6 +440,22 @@ func (r *apiKeyRepository) ListByUserID(ctx context.Context, userID int64, param
return outKeys, paginationResultFromTotal(int64(total), params), nil
}
func (r *apiKeyRepository) ListAllByUserID(ctx context.Context, userID int64, filters service.APIKeyListFilters) ([]service.APIKey, error) {
keys, err := r.apiKeyListByUserIDQuery(userID, filters).
WithGroup().
Order(dbent.Asc(apikey.FieldID)).
All(ctx)
if err != nil {
return nil, err
}
outKeys := make([]service.APIKey, 0, len(keys))
for i := range keys {
outKeys = append(outKeys, *apiKeyEntityToService(keys[i]))
}
return outKeys, nil
}
func (r *apiKeyRepository) VerifyOwnership(ctx context.Context, userID int64, apiKeyIDs []int64) ([]int64, error) {
if len(apiKeyIDs) == 0 {
return []int64{}, nil
@@ -504,14 +525,24 @@ func apiKeyListOrder(params pagination.PaginationParams) []func(*entsql.Selector
field = apikey.FieldLastUsedAt
case "created_at":
field = apikey.FieldCreatedAt
case "id":
field = apikey.FieldID
default:
field = apikey.FieldID
}
if sortOrder == pagination.SortOrderAsc {
return []func(*entsql.Selector){dbent.Asc(field), dbent.Asc(apikey.FieldID)}
orders := []func(*entsql.Selector){dbent.Asc(field)}
if field != apikey.FieldID {
orders = append(orders, dbent.Asc(apikey.FieldID))
}
return orders
}
return []func(*entsql.Selector){dbent.Desc(field), dbent.Desc(apikey.FieldID)}
orders := []func(*entsql.Selector){dbent.Desc(field)}
if field != apikey.FieldID {
orders = append(orders, dbent.Desc(apikey.FieldID))
}
return orders
}
// SearchAPIKeys searches API keys by user ID and/or keyword (name)
@@ -23,3 +23,20 @@ func (s *APIKeyRepoSuite) TestListByUserID_SortByNameAsc() {
s.Require().Equal("a-key", keys[0].Name)
s.Require().Equal("z-key", keys[1].Name)
}
func (s *APIKeyRepoSuite) TestListByUserID_SortByID() {
user := s.mustCreateUser("sort-id@example.com")
first := s.mustCreateApiKey(user.ID, "sk-id-a", "a-key", nil)
second := s.mustCreateApiKey(user.ID, "sk-id-b", "b-key", nil)
keys, _, err := s.repo.ListByUserID(s.ctx, user.ID, pagination.PaginationParams{
Page: 1,
PageSize: 10,
SortBy: "id",
SortOrder: "desc",
}, service.APIKeyListFilters{})
s.Require().NoError(err)
s.Require().Len(keys, 2)
s.Require().Equal(second.ID, keys[0].ID)
s.Require().Equal(first.ID, keys[1].ID)
}