mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(api): split Provider into ProviderGroup and APIKey models
Restructure the provider management system by separating the monolithic Provider model into two distinct entities: - ProviderGroup: defines shared upstream configuration (type, base_url, google settings, models, status) - APIKey: represents individual credentials within a group (api_key, weight, status, auto_ban, ban settings) This change also updates: - Binding model to reference GroupID instead of RouteGroup string - All CRUD handlers for the new provider-group and api-key endpoints - Sync service to rebuild provider snapshots from joined tables - Model registry to aggregate capabilities across group/key pairs - Access handler to validate namespace existence and subset constraints - Migration importer to handle the new schema structure - All related tests to use the new model relationships BREAKING CHANGE: Provider API endpoints replaced with /provider-groups and /api-keys endpoints; Binding.RouteGroup replaced with Binding.GroupID
This commit is contained in:
@@ -104,9 +104,9 @@ func (h *AdminHandler) BatchMasters(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
// BatchProviders godoc
|
||||
// @Summary Batch providers
|
||||
// @Description Batch delete or status update for providers
|
||||
// BatchAPIKeys godoc
|
||||
// @Summary Batch api keys
|
||||
// @Description Batch delete or status update for api keys
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
@@ -115,8 +115,8 @@ func (h *AdminHandler) BatchMasters(c *gin.Context) {
|
||||
// @Success 200 {object} BatchResponse
|
||||
// @Failure 400 {object} gin.H
|
||||
// @Failure 500 {object} gin.H
|
||||
// @Router /admin/providers/batch [post]
|
||||
func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
// @Router /admin/api-keys/batch [post]
|
||||
func (h *Handler) BatchAPIKeys(c *gin.Context) {
|
||||
var req BatchActionRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body"})
|
||||
@@ -140,8 +140,8 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
needsBindingSync := false
|
||||
for _, id := range req.IDs {
|
||||
var p model.Provider
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
var key model.APIKey
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: "not found"})
|
||||
continue
|
||||
@@ -151,11 +151,7 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
switch action {
|
||||
case "delete":
|
||||
if err := h.db.Delete(&p).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.sync.SyncProviderDelete(&p); err != nil {
|
||||
if err := h.db.Delete(&key).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
@@ -167,15 +163,11 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
update["ban_reason"] = ""
|
||||
update["ban_until"] = nil
|
||||
}
|
||||
if err := h.db.Model(&p).Updates(update).Error; err != nil {
|
||||
if err := h.db.Model(&key).Updates(update).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.db.First(&p, id).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
if err := h.sync.SyncProvider(&p); err != nil {
|
||||
if err := h.db.First(&key, id).Error; err != nil {
|
||||
resp.Failed = append(resp.Failed, BatchResult{ID: id, Error: err.Error()})
|
||||
continue
|
||||
}
|
||||
@@ -184,6 +176,10 @@ func (h *Handler) BatchProviders(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
if needsBindingSync {
|
||||
if err := h.sync.SyncProviders(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync providers", "details": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.sync.SyncBindings(h.db); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to sync bindings", "details": err.Error()})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user