feat(api): allow batch status updates

This commit is contained in:
zenfun
2025-12-21 23:32:07 +08:00
parent c2ed2f3f9e
commit fa7f92c6e3
5 changed files with 337 additions and 18 deletions

View File

@@ -200,3 +200,44 @@ func TestBatchModels_Delete(t *testing.T) {
t.Fatalf("expected meta removed, got %q", got)
}
}
func TestBatchBindings_Status(t *testing.T) {
h, db := newTestHandler(t)
b := &model.Binding{
Namespace: "ns",
PublicModel: "m1",
RouteGroup: "default",
SelectorType: "exact",
SelectorValue: "m1",
Status: "active",
}
if err := db.Create(b).Error; err != nil {
t.Fatalf("create binding: %v", err)
}
r := gin.New()
r.POST("/admin/bindings/batch", h.BatchBindings)
payload := map[string]any{
"action": "status",
"status": "inactive",
"ids": []uint{b.ID},
}
bb, _ := json.Marshal(payload)
req := httptest.NewRequest(http.MethodPost, "/admin/bindings/batch", bytes.NewReader(bb))
req.Header.Set("Content-Type", "application/json")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d body=%s", rr.Code, rr.Body.String())
}
var updated model.Binding
if err := db.First(&updated, b.ID).Error; err != nil {
t.Fatalf("reload binding: %v", err)
}
if updated.Status != "inactive" {
t.Fatalf("expected status inactive, got %q", updated.Status)
}
}