refactor: simplify document deletion logic and remove redundant chunk handling
This commit is contained in:
@@ -120,10 +120,6 @@ func (s *index) IndexFAQByID(ctx context.Context, faqID int64) error {
|
||||
|
||||
func (s *index) RemoveDocumentIndex(ctx context.Context, documentID int64) error {
|
||||
chunks := repositories.KnowledgeChunkRepository.FindByDocumentID(sqls.DB(), documentID)
|
||||
return s.RemoveDocumentIndexByChunks(ctx, documentID, chunks)
|
||||
}
|
||||
|
||||
func (s *index) RemoveDocumentIndexByChunks(ctx context.Context, documentID int64, chunks []models.KnowledgeChunk) error {
|
||||
if len(chunks) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -167,18 +167,13 @@ func (s *knowledgeDocumentService) UpdateKnowledgeDocument(req request.UpdateKno
|
||||
}
|
||||
|
||||
func (s *knowledgeDocumentService) DeleteKnowledgeDocument(id int64) error {
|
||||
chunks := repositories.KnowledgeChunkRepository.FindByDocumentID(sqls.DB(), id)
|
||||
if err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
|
||||
_ = repositories.KnowledgeDocumentRepository.Updates(ctx.Tx, id, map[string]any{
|
||||
"status": enums.StatusDeleted,
|
||||
"updated_at": time.Now(),
|
||||
})
|
||||
ctx.Tx.Delete(&models.KnowledgeChunk{}, "document_id = ?", id)
|
||||
return nil
|
||||
if err := repositories.KnowledgeDocumentRepository.Updates(sqls.DB(), id, map[string]any{
|
||||
"status": enums.StatusDeleted,
|
||||
"updated_at": time.Now(),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
return rag.Index.RemoveDocumentIndexByChunks(context.Background(), id, chunks)
|
||||
return rag.Index.RemoveDocumentIndex(context.Background(), id)
|
||||
}
|
||||
|
||||
func (s *knowledgeDocumentService) buildKnowledgeDocumentModel(req request.CreateKnowledgeDocumentRequest) (*models.KnowledgeDocument, error) {
|
||||
|
||||
Reference in New Issue
Block a user