Files
yes.blog/Yes.Application/Admins/Comments/GetLatestCommentsQueryHandler.cs
2025-04-30 09:51:36 +08:00

35 lines
1.2 KiB
C#

namespace Yes.Application.Admins.Comments
{
public record GetLatestCommentsQuery() : IRequest<List<GetLatestCommentsQueryResponse>>;
public record GetLatestCommentsQueryResponse(
int Id,
int ArticleId,
string Content,
DateTime CreateDate,
bool HasVerified
);
public class GetLatestCommentsQueryHandler(BlogDbContext db) : IRequestHandler<GetLatestCommentsQuery, List<GetLatestCommentsQueryResponse>>
{
private readonly BlogDbContext _db = db;
public async Task<List<GetLatestCommentsQueryResponse>> Handle(GetLatestCommentsQuery request, CancellationToken cancellationToken)
{
var query = _db.Comments.AsNoTracking().OrderByDescending(x => x.Id).Select(comment =>
new GetLatestCommentsQueryResponse(
comment.Id,
comment.ArticleId,
comment.Content,
comment.CreateDate,
comment.Status== CommentStatusEnum.审核通过
)
).Take(10);
var comments = await query.ToListAsync(cancellationToken);
return comments;
}
}
}