2024-07-14 17:22:40 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
2024-07-15 18:05:02 +00:00
|
|
|
"context"
|
2024-07-15 18:07:10 +00:00
|
|
|
"errors"
|
2024-07-14 17:22:40 +00:00
|
|
|
"leafdev.top/leaf/rag/ent"
|
|
|
|
"leafdev.top/leaf/rag/ent/library"
|
|
|
|
"leafdev.top/leaf/rag/internal/providers"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LibraryLogic struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
var client = providers.MustGet[ent.Client]()
|
|
|
|
|
|
|
|
func NewLibraryLogic() *LibraryLogic {
|
|
|
|
return &LibraryLogic{}
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:05:02 +00:00
|
|
|
func (l *LibraryLogic) ListLibrary(ctx context.Context) ([]*ent.Library, error) {
|
2024-07-14 17:22:40 +00:00
|
|
|
return client.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx)
|
|
|
|
}
|
|
|
|
|
2024-07-15 18:05:02 +00:00
|
|
|
func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Library, error) {
|
2024-07-15 18:07:10 +00:00
|
|
|
// name 不能为空
|
|
|
|
if name == "" {
|
|
|
|
return nil, errors.New("名称不能为空")
|
|
|
|
}
|
|
|
|
|
2024-07-14 17:22:40 +00:00
|
|
|
return client.Library.Create().
|
|
|
|
SetName(name).
|
|
|
|
SetUserID(GetUserId(ctx)).
|
|
|
|
Save(ctx)
|
|
|
|
}
|