fix(knowledge): paginate document list without content
This commit is contained in:
@@ -54,6 +54,26 @@ func BuildKnowledgeDocument(item *models.KnowledgeDocument) response.KnowledgeDo
|
||||
}
|
||||
}
|
||||
|
||||
func BuildKnowledgeDocumentList(item *models.KnowledgeDocument) response.KnowledgeDocumentListResponse {
|
||||
return response.KnowledgeDocumentListResponse{
|
||||
ID: item.ID,
|
||||
KnowledgeBaseID: item.KnowledgeBaseID,
|
||||
Title: item.Title,
|
||||
Status: item.Status,
|
||||
StatusName: enums.GetStatusLabel(item.Status),
|
||||
IndexStatus: item.IndexStatus,
|
||||
IndexStatusName: enums.GetKnowledgeDocumentIndexStatusLabel(item.IndexStatus),
|
||||
IndexedAt: item.IndexedAt,
|
||||
IndexError: item.IndexError,
|
||||
ContentHash: item.ContentHash,
|
||||
ContentType: item.ContentType,
|
||||
CreatedAt: item.CreatedAt,
|
||||
UpdatedAt: item.UpdatedAt,
|
||||
CreateUserName: item.CreateUserName,
|
||||
UpdateUserName: item.UpdateUserName,
|
||||
}
|
||||
}
|
||||
|
||||
func BuildKnowledgeFAQ(item *models.KnowledgeFAQ) response.KnowledgeFAQResponse {
|
||||
return response.KnowledgeFAQResponse{
|
||||
ID: item.ID,
|
||||
|
||||
@@ -39,10 +39,10 @@ func (c *KnowledgeDocumentController) AnyList() *web.JsonResult {
|
||||
cnd.Where("index_status = ?", indexStatus)
|
||||
}
|
||||
|
||||
list, paging := services.KnowledgeDocumentService.FindPageByCnd(cnd)
|
||||
results := make([]response.KnowledgeDocumentResponse, 0, len(list))
|
||||
list, paging := services.KnowledgeDocumentService.FindPageListByCnd(cnd)
|
||||
results := make([]response.KnowledgeDocumentListResponse, 0, len(list))
|
||||
for _, item := range list {
|
||||
results = append(results, builders.BuildKnowledgeDocument(&item))
|
||||
results = append(results, builders.BuildKnowledgeDocumentList(&item))
|
||||
}
|
||||
return web.JsonData(&web.PageResult{Results: results, Page: paging})
|
||||
}
|
||||
|
||||
@@ -51,6 +51,25 @@ type KnowledgeDocumentResponse struct {
|
||||
UpdateUserName string `json:"updateUserName"`
|
||||
}
|
||||
|
||||
type KnowledgeDocumentListResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
KnowledgeBaseID int64 `json:"knowledgeBaseId"`
|
||||
KnowledgeBaseName string `json:"knowledgeBaseName,omitempty"`
|
||||
Title string `json:"title"`
|
||||
ContentType enums.KnowledgeDocumentContentType `json:"contentType"`
|
||||
Status enums.Status `json:"status"`
|
||||
StatusName string `json:"statusName"`
|
||||
IndexStatus enums.KnowledgeDocumentIndexStatus `json:"indexStatus"`
|
||||
IndexStatusName string `json:"indexStatusName"`
|
||||
IndexedAt *time.Time `json:"indexedAt"`
|
||||
IndexError string `json:"indexError"`
|
||||
ContentHash string `json:"contentHash"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
CreateUserName string `json:"createUserName"`
|
||||
UpdateUserName string `json:"updateUserName"`
|
||||
}
|
||||
|
||||
type KnowledgeFAQResponse struct {
|
||||
ID int64 `json:"id"`
|
||||
KnowledgeBaseID int64 `json:"knowledgeBaseId"`
|
||||
|
||||
@@ -62,6 +62,18 @@ func (r *knowledgeDocumentRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd)
|
||||
return
|
||||
}
|
||||
|
||||
func (r *knowledgeDocumentRepository) FindPageListByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.KnowledgeDocument, paging *sqls.Paging) {
|
||||
cnd.Find(db.Omit("content"), &list)
|
||||
count := cnd.Count(db, &models.KnowledgeDocument{})
|
||||
|
||||
paging = &sqls.Paging{
|
||||
Page: cnd.Paging.Page,
|
||||
Limit: cnd.Paging.Limit,
|
||||
Total: count,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r *knowledgeDocumentRepository) Count(db *gorm.DB, cnd *sqls.Cnd) int64 {
|
||||
return cnd.Count(db, &models.KnowledgeDocument{})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"cs-agent/internal/models"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
|
||||
"github.com/glebarez/sqlite"
|
||||
"github.com/mlogclub/simple/sqls"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func TestKnowledgeDocumentRepositoryFindPageListByCndOmitsContentAndPaginates(t *testing.T) {
|
||||
db, err := gorm.Open(sqlite.Open("file:knowledge_document_repository_test?mode=memory&cache=shared"), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite error = %v", err)
|
||||
}
|
||||
if err := db.AutoMigrate(&models.KnowledgeDocument{}); err != nil {
|
||||
t.Fatalf("auto migrate error = %v", err)
|
||||
}
|
||||
if err := db.Exec("DELETE FROM knowledge_documents").Error; err != nil {
|
||||
t.Fatalf("clean knowledge documents error = %v", err)
|
||||
}
|
||||
sqls.SetDB(db)
|
||||
|
||||
for i := 1; i <= 3; i++ {
|
||||
item := &models.KnowledgeDocument{
|
||||
KnowledgeBaseID: 1,
|
||||
Title: fmt.Sprintf("doc-%d", i),
|
||||
ContentType: enums.KnowledgeDocumentContentTypeMarkdown,
|
||||
Content: strings.Repeat("large document content ", 200),
|
||||
Status: enums.StatusOk,
|
||||
IndexStatus: enums.KnowledgeDocumentIndexStatusPending,
|
||||
}
|
||||
if err := db.Create(item).Error; err != nil {
|
||||
t.Fatalf("create document %d error = %v", i, err)
|
||||
}
|
||||
}
|
||||
|
||||
list, paging := KnowledgeDocumentRepository.FindPageListByCnd(db, sqls.NewCnd().Eq("knowledge_base_id", 1).Asc("id").Page(1, 2))
|
||||
if len(list) != 2 {
|
||||
t.Fatalf("len(list) = %d, want 2", len(list))
|
||||
}
|
||||
if paging.Total != 3 {
|
||||
t.Fatalf("paging.Total = %d, want 3", paging.Total)
|
||||
}
|
||||
for _, item := range list {
|
||||
if item.Content != "" {
|
||||
t.Fatalf("list item content should be empty, got %q", item.Content)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -54,6 +54,10 @@ func (s *knowledgeDocumentService) FindPageByCnd(cnd *sqls.Cnd) (list []models.K
|
||||
return repositories.KnowledgeDocumentRepository.FindPageByCnd(sqls.DB(), cnd)
|
||||
}
|
||||
|
||||
func (s *knowledgeDocumentService) FindPageListByCnd(cnd *sqls.Cnd) (list []models.KnowledgeDocument, paging *sqls.Paging) {
|
||||
return repositories.KnowledgeDocumentRepository.FindPageListByCnd(sqls.DB(), cnd)
|
||||
}
|
||||
|
||||
func (s *knowledgeDocumentService) Count(cnd *sqls.Cnd) int64 {
|
||||
return repositories.KnowledgeDocumentRepository.Count(sqls.DB(), cnd)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user