This commit is contained in:
Sakurasan
2025-02-01 23:52:55 +08:00
parent 65d6d12972
commit bc223d6530
30 changed files with 2683 additions and 242 deletions
+17 -1
View File
@@ -18,6 +18,7 @@ type ApiKeyRepository interface {
Update(apiKey *model.ApiKey) error
Delete(id int64) error
List(offset, limit int, status *int) ([]model.ApiKey, error)
ListWithFilters(offset, limit int, filters map[string]interface{}) ([]model.ApiKey, int64, error)
Enable(id int64) error
Disable(id int64) error
BatchEnable(ids []int64) error
@@ -77,7 +78,7 @@ func (dao *ApiKeyDAO) Update(apiKey *model.ApiKey) error {
if apiKey == nil {
return errors.New("apiKey is nil")
}
apiKey.UpdatedAt = time.Now()
apiKey.UpdatedAt = time.Now().Unix()
return dao.db.Save(apiKey).Error
}
@@ -100,6 +101,21 @@ func (dao *ApiKeyDAO) List(offset, limit int, status *int) ([]model.ApiKey, erro
return apiKeys, nil
}
// ListApiKeysWithFilters 根据条件获取ApiKey列表
func (dao *ApiKeyDAO) ListWithFilters(offset, limit int, filters map[string]interface{}) ([]model.ApiKey, int64, error) {
var apiKeys []model.ApiKey
db := dao.db.Offset(offset).Limit(limit)
for key, value := range filters {
db = db.Where(key+" = ?", value)
}
var count int64
err := db.Find(&apiKeys).Count(&count).Error
if err != nil {
return nil, 0, err
}
return apiKeys, count, nil
}
// EnableApiKey 启用ApiKey
func (dao *ApiKeyDAO) Enable(id int64) error {
return dao.db.Model(&model.ApiKey{}).Where("id = ?", id).Update("status", 0).Error