rag/internal/logic/library.go

35 lines
738 B
Go
Raw Normal View History

package logic
import (
2024-07-15 18:05:02 +00:00
"context"
2024-07-15 18:07:10 +00:00
"errors"
"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) {
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("名称不能为空")
}
return client.Library.Create().
SetName(name).
SetUserID(GetUserId(ctx)).
Save(ctx)
}