This commit is contained in:
code@server 2025-12-18 01:23:06 +08:00
parent 76308c41c4
commit 08ed928fb9
2 changed files with 32 additions and 34 deletions

View File

@ -21,61 +21,58 @@ public class BookmarkService : IBookmarkService
/// <inheritdoc />
public async Task<List<BookmarkDto>> GetUserBookmarksAsync(Guid userId, Guid deviceId, bool isAdminDevice, string? tag = null)
{
var query = _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId);
// 先查询所有书签,然后在内存中过滤
// 避免 FreeSql 无法正确转换 AllowedDevices.Contains 和 Tags.Contains 为 SQL
var bookmarks = await _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId)
.OrderByDescending(b => b.Order)
.ToListAsync();
// 如果不是管理员设备,需要过滤可见性
if (!isAdminDevice)
{
query = query.Where(b =>
bookmarks = bookmarks.Where(b =>
b.Visibility == VisibilityType.Public ||
(b.Visibility == VisibilityType.Specified && b.AllowedDevices != null && b.AllowedDevices.Contains(deviceId))
);
).ToList();
}
// 按标签筛选
if (!string.IsNullOrEmpty(tag))
{
query = query.Where(b => b.Tags.Contains(tag));
bookmarks = bookmarks.Where(b => b.Tags.Contains(tag)).ToList();
}
var bookmarks = await query
.OrderByDescending(b => b.Order)
.ToListAsync();
return bookmarks.Select(MapToDto).ToList();
}
/// <inheritdoc />
public async Task<List<BookmarkDto>> SearchBookmarksAsync(Guid userId, Guid deviceId, bool isAdminDevice, string keyword)
{
var query = _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId);
// 先在数据库层面进行关键词搜索,然后在内存中过滤可见性
// 避免 FreeSql 无法正确转换 AllowedDevices.Contains 为 SQL
var lowerKeyword = keyword.ToLower();
var bookmarks = await _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId)
.Where(b =>
b.Title.ToLower().Contains(lowerKeyword) ||
b.Url.ToLower().Contains(lowerKeyword) ||
(b.Description != null && b.Description.ToLower().Contains(lowerKeyword))
)
.OrderByDescending(b => b.VisitCount)
.OrderByDescending(b => b.Order)
.ToListAsync();
// 如果不是管理员设备,需要过滤可见性
if (!isAdminDevice)
{
query = query.Where(b =>
bookmarks = bookmarks.Where(b =>
b.Visibility == VisibilityType.Public ||
(b.Visibility == VisibilityType.Specified && b.AllowedDevices != null && b.AllowedDevices.Contains(deviceId))
);
).ToList();
}
// 搜索关键词
var lowerKeyword = keyword.ToLower();
query = query.Where(b =>
b.Title.ToLower().Contains(lowerKeyword) ||
b.Url.ToLower().Contains(lowerKeyword) ||
(b.Description != null && b.Description.ToLower().Contains(lowerKeyword))
);
var bookmarks = await query
.OrderByDescending(b => b.VisitCount)
.OrderByDescending(b => b.Order)
.Take(20)
.ToListAsync();
return bookmarks.Select(MapToDto).ToList();
return bookmarks.Take(20).Select(MapToDto).ToList();
}
/// <inheritdoc />

View File

@ -20,20 +20,21 @@ public class TagService : ITagService
/// <inheritdoc />
public async Task<List<TagDto>> GetUserTagsAsync(Guid userId, Guid deviceId, bool isAdminDevice)
{
var query = _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId);
// 先查询所有书签,然后在内存中过滤
// 避免 FreeSql 无法正确转换 AllowedDevices.Contains 为 SQL
var bookmarks = await _freeSql.Select<Bookmark>()
.Where(b => b.UserId == userId)
.ToListAsync();
// 如果不是管理员设备,需要过滤可见性
if (!isAdminDevice)
{
query = query.Where(b =>
bookmarks = bookmarks.Where(b =>
b.Visibility == VisibilityType.Public ||
(b.Visibility == VisibilityType.Specified && b.AllowedDevices != null && b.AllowedDevices.Contains(deviceId))
);
).ToList();
}
var bookmarks = await query.ToListAsync();
// 统计标签使用数量
var tagCounts = new Dictionary<string, int>();