rag/internal/logic/document.go
2024-07-21 01:20:52 +08:00

58 lines
1.2 KiB
Go

package logic
import (
"context"
"errors"
"leafdev.top/leaf/rag/ent"
"leafdev.top/leaf/rag/ent/document"
)
type DocumentLogic struct {
LibraryLogic LibraryLogic
}
func NewDocumentLogic() *DocumentLogic {
return &DocumentLogic{
LibraryLogic: *NewLibraryLogic(),
}
}
func (d DocumentLogic) ListDocument(ctx context.Context, libraryId int64) ([]*ent.Document, error) {
library, err := d.LibraryLogic.ExistsByUser(ctx, int(libraryId))
if err != nil {
return nil, err
}
if !library {
return nil, errors.New("我们在你的账户下找不到这个资料库")
}
return orm.Document.Query().Where(
document.UserIDEQ(GetUserId(ctx)),
document.LibraryIDEQ(libraryId),
).All(ctx)
}
func (d DocumentLogic) CreateDocument(ctx context.Context, libraryId int64, name string) (*ent.Document, error) {
// 检查 Library 是否存在
exists, err := d.LibraryLogic.ExistsByUser(ctx, int(libraryId))
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("library not exists")
}
if name == "" {
return nil, errors.New("name cannot be empty")
}
return orm.Document.Create().
SetName(name).
SetUserID(GetUserId(ctx)).
SetLibraryID(libraryId).
Save(ctx)
}