perf: 优化主题查询

This commit is contained in:
橙子
2025-01-18 01:07:38 +08:00
parent 6482218a68
commit 3be5675828
3 changed files with 22 additions and 11 deletions

View File

@@ -76,7 +76,6 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
var entities = await _articleRepository.GetTreeAsync(x => x.DiscussId == discussId);
//var result = entities.Tile();
var items = entities.Adapt<List<ArticleAllOutputDto>>();
return items;
}

View File

@@ -72,8 +72,7 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
.Select((discuss, user, info, plate) => new DiscussGetOutputDto
{
Id = discuss.Id,
IsAgree = SqlFunc.Subqueryable<AgreeEntity>().WhereIF(CurrentUser.Id != null,
x => x.CreatorId == CurrentUser.Id && x.DiscussId == discuss.Id).Any(),
IsAgree = false,
User = new BbsUserGetListOutputDto()
{
UserName = user.UserName,
@@ -94,15 +93,25 @@ namespace Yi.Framework.Bbs.Application.Services.Forum
Logo = plate.Logo
}
}, true)
.SingleAsync(discuss => discuss.Id == id);
.FirstAsync(discuss => discuss.Id == id);
if (item is not null)
if (item is null)
{
throw new UserFriendlyException("该主题不存在", "404");
}
//组装点赞
var agreeCreatorList =
(await _agreeRepository._DbQueryable.Where(x => x.DiscussId == item.Id).Select(x=>x.CreatorId).ToListAsync());
//已登录
if (CurrentUser.Id is not null)
{
item.IsAgree = agreeCreatorList.Contains(CurrentUser.Id);
}
await VerifyDiscussPermissionAsync(item.Id);
await _localEventBus.PublishAsync(new SeeDiscussEventArgs
{ DiscussId = item.Id, OldSeeNum = item.SeeNum });
}
return item;
}

View File

@@ -8,9 +8,12 @@ namespace Yi.Framework.Bbs.Domain.Entities.Forum
{
[SugarTable("Discuss")]
[SugarIndex($"index_{nameof(Title)}", nameof(Title), OrderByType.Asc)]
[SugarIndex($"index_{nameof(PlateId)}", nameof(PlateId), OrderByType.Asc)]
[SugarIndex($"index_{nameof(CreatorId)}", nameof(CreatorId), OrderByType.Asc)]
[SugarIndex($"index_{nameof(CreationTime)}", nameof(CreationTime), OrderByType.Desc)]
[SugarIndex($"index_{nameof(IsDeleted)}_{nameof(PlateId)}_{nameof(CreatorId)}",
nameof(IsDeleted), OrderByType.Asc,
nameof(PlateId), OrderByType.Asc,
nameof(CreatorId), OrderByType.Asc
)]
public class DiscussAggregateRoot : AggregateRoot<Guid>, ISoftDelete, IAuditedObject
{
public DiscussAggregateRoot()