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

60 lines
1.5 KiB
Go

package logic
import (
"context"
"errors"
"leafdev.top/leaf/rag/ent"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/internal/providers/helper"
)
type LibraryLogic struct {
}
var pageSize = 10
func NewLibraryLogic() *LibraryLogic {
return &LibraryLogic{}
}
func (l *LibraryLogic) ListLibrary(ctx context.Context, page int) ([]*ent.Library, error) {
return orm.Library.Query().Where(library.UserID(GetUserId(ctx))).
Offset(helper.Offset(page, pageSize)).
Limit(pageSize).
All(ctx)
}
func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Library, error) {
// name 不能为空
if name == "" {
return nil, errors.New("名称不能为空")
}
return orm.Library.Create().
SetName(name).
SetUserID(GetUserId(ctx)).
Save(ctx)
}
func (l *LibraryLogic) DeleteLibrary(ctx context.Context, id int) error {
return orm.Library.DeleteOneID(id).Exec(ctx)
}
func (l *LibraryLogic) UpdateLibrary(ctx context.Context, id int, name string) (*ent.Library, error) {
return orm.Library.UpdateOneID(id).
SetName(name).
Save(ctx)
}
func (l *LibraryLogic) GetLibrary(ctx context.Context, id int) (*ent.Library, error) {
return orm.Library.Get(ctx, id)
}
func (l *LibraryLogic) Exists(ctx context.Context, id int) (bool, error) {
return orm.Library.Query().Where(library.ID(id)).Exist(ctx)
}
func (l *LibraryLogic) ExistsByUser(ctx context.Context, libraryId int) (bool, error) {
return orm.Library.Query().Where(library.IDEQ(libraryId), library.UserIDEQ(GetUserId(ctx))).Exist(ctx)
}