Skip to content

Commit

Permalink
add user like article query
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 18, 2024
1 parent 7ccf8cf commit e27a27a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/LinCms.Application.Contracts/Blog/Articles/ArticleSearchDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ public class ArticleSearchDto : PageDto
/// </summary>
public Guid? CollectionId { get; set; }

public ArticleSearchTypeEnum? ArticleSearchType { get; set; }
public override string ToString()
{
return $"{ClassifyId}:{ChannelId}:{TagId}:{Title}:{UserId}:{Count}:{Page}:{Sort}";
}

}


public enum ArticleSearchTypeEnum
{
/// <summary>
/// 点赞
/// </summary>
Like,
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 19 additions & 4 deletions src/LinCms.Application/Blog/Articles/ArticleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class ArticleService(IAuditBaseRepository<Article> articleRepository,

#region CRUD

/// <inheritdoc />
public async Task<PagedResultDto<ArticleListDto>> GetArticleAsync(ArticleSearchDto searchDto)
{
DateTime monthDays = DateTime.Now.AddDays(-30);
Expand All @@ -60,6 +61,18 @@ public async Task<PagedResultDto<ArticleListDto>> GetArticleAsync(ArticleSearchD
}

long? userId = CurrentUser.FindUserId();

List<Guid> likedArticleIds = new();
bool isAnyLiked = false;

if (searchDto.ArticleSearchType == ArticleSearchTypeEnum.Like)
{
likedArticleIds = await userLikeRepository.Select
.Where(r => r.CreateUserId == userId && r.SubjectType == UserLikeSubjectType.UserLikeArticle)
.ToListAsync(r => r.SubjectId);
isAnyLiked = searchDto.ArticleSearchType == ArticleSearchTypeEnum.Like && likedArticleIds.Any();
}

List<Article> articles = await articleRepository
.Select
.Include(r => r.UserInfo)
Expand All @@ -76,12 +89,14 @@ public async Task<PagedResultDto<ArticleListDto>> GetArticleAsync(ArticleSearchD
.WhereIf(searchDto.Sort == "WEEKLY_HOTTEST", r => r.CreateTime > weeklyDays)
.WhereIf(searchDto.Sort == "MONTHLY_HOTTEST", r => r.CreateTime > monthDays)
.WhereIf(searchDto.CollectionId != null, r => articleIds.Contains(r.Id))
.WhereIf(isAnyLiked, r => likedArticleIds.Contains(r.Id))
.OrderByDescending(
searchDto.Sort == "THREE_DAYS_HOTTEST" || searchDto.Sort == "WEEKLY_HOTTEST" ||
searchDto.Sort == "MONTHLY_HOTTEST" ||
searchDto.Sort == "HOTTEST",
r => r.ViewHits + (r.LikesQuantity * 20) + (r.CommentQuantity * 30))
.OrderByDescending(r => r.CreateTime).ToPagerListAsync(searchDto, out long totalCount);
.OrderByDescending(r => r.CreateTime)
.ToPagerListAsync(searchDto, out long totalCount);

List<ArticleListDto> articleDtos = articles
.Select(a =>
Expand Down Expand Up @@ -111,7 +126,7 @@ public async Task DeleteAsync(Guid id)
}
}

await articleRepository.DeleteAsync(new Article { Id = id });
await articleRepository.DeleteAsync(new Article {Id = id});
await tagArticleRepository.DeleteAsync(r => r.ArticleId == id);
await commentRepository.DeleteAsync(r => r.SubjectId == id);
await userLikeRepository.DeleteAsync(r => r.SubjectId == id);
Expand Down Expand Up @@ -160,7 +175,7 @@ public async Task<Guid> CreateAsync(CreateUpdateArticleDto createArticle)
Article article = Mapper.Map<Article>(createArticle);
article.Archive = DateTime.Now.ToString("yyy年MM月");
article.WordNumber = createArticle.Content.Length;
article.ReadingTime = (long)TextAnalysisUtil.GetReadingTime(createArticle.Content).Minutes;
article.ReadingTime = (long) TextAnalysisUtil.GetReadingTime(createArticle.Content).Minutes;

article.Tags = new List<Tag>();
foreach (var articleTagId in createArticle.TagIds)
Expand Down Expand Up @@ -209,7 +224,7 @@ public async Task UpdateAsync(Guid id, CreateUpdateArticleDto updateArticleDto)

Mapper.Map(updateArticleDto, article);
article.WordNumber = article.Content.Length;
article.ReadingTime = (long)TextAnalysisUtil.GetReadingTime(article.Content).Minutes;
article.ReadingTime = (long) TextAnalysisUtil.GetReadingTime(article.Content).Minutes;
await articleRepository.UpdateAsync(article);

ArticleDraft articleDraft = Mapper.Map<ArticleDraft>(article);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ namespace LinCms.Blog.AuthorCenter
/// 创建者中心统计
/// </summary>
public class AuthorCenterService(IAuditBaseRepository<Article> articleRepository,
IAuditBaseRepository<UserLike> userLikeRepository,
IAuditBaseRepository<Comment> commentRepository)
IAuditBaseRepository<UserLike> userLikeRepository)
: ApplicationService, IAuthorCenterService
{
private readonly IAuditBaseRepository<Comment> _commentRepository = commentRepository;

public async Task<ArticleCardDto> GetArtcileCardAsync()
{
long? userid = CurrentUser.FindUserId();
Expand Down
5 changes: 5 additions & 0 deletions src/LinCms.Web/LinCms.Web.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e27a27a

Please sign in to comment.