Files
ragflow/internal/dao/user_canvas_version.go

47 lines
1.6 KiB
Go
Raw Normal View History

feat: implement GET /api/v1/agents/<agent_id>/versions API (#15629) ## Summary Implement the `GET /api/v1/agents/<agent_id>/versions` endpoint in Go, listing all version snapshots for an agent canvas in descending update time order. ### Changes - **New**: `internal/dao/user_canvas_version.go` — `UserCanvasVersionDAO` with `ListByCanvasID` (ordered by update_time DESC) and `GetByID` - **Modified**: `internal/service/agent.go` — Added `CheckCanvasAccess`, `ListVersions`, `GetVersion` methods - **Modified**: `internal/handler/agent.go` — Added `ListAgentVersions` handler with auth check - **Modified**: `internal/router/router.go` — Registered `GET /:agent_id/versions` route - **New**: `internal/service/agent_test.go` — 5 service-level tests (SQLite in-memory DB, zero mock) - **Modified**: `internal/handler/agent_test.go` — 3 handler-level tests (real DB, pre-authenticated context) ### Testing All 8 tests pass with zero mocking (in-memory SQLite replaces MySQL): ``` === RUN TestListVersions_Success --- PASS === RUN TestListVersions_Empty --- PASS === RUN TestCheckCanvasAccess_Owner --- PASS === RUN TestCheckCanvasAccess_NotOwner --- PASS === RUN TestCheckCanvasAccess_NotFound --- PASS === RUN TestListAgentVersionsHandler_Success --- PASS === RUN TestListAgentVersionsHandler_NoPermission --- PASS === RUN TestListAgentVersionsHandler_CanvasNotFound --- PASS ``` Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-04 15:36:26 +08:00
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package dao
import "ragflow/internal/entity"
// UserCanvasVersionDAO user canvas version data access object
type UserCanvasVersionDAO struct{}
// NewUserCanvasVersionDAO create user canvas version DAO
func NewUserCanvasVersionDAO() *UserCanvasVersionDAO {
return &UserCanvasVersionDAO{}
}
// ListByCanvasID returns all versions for a canvas, ordered by update_time DESC.
func (d *UserCanvasVersionDAO) ListByCanvasID(canvasID string) ([]*entity.UserCanvasVersion, error) {
var versions []*entity.UserCanvasVersion
err := DB.Select("id", "user_canvas_id", "title", "create_time", "update_time", "create_date", "update_date").
Where("user_canvas_id = ?", canvasID).
Order("update_time DESC").
Find(&versions).Error
return versions, err
}
// GetByID returns a version by its ID.
func (d *UserCanvasVersionDAO) GetByID(versionID string) (*entity.UserCanvasVersion, error) {
var version entity.UserCanvasVersion
err := DB.Where("id = ?", versionID).First(&version).Error
if err != nil {
return nil, err
}
return &version, nil
}