首页 技术 正文
技术 2022年11月10日
0 收藏 615 点赞 3,640 浏览 5785 个字

介绍

评论本来是要放到标签里面去讲的,但是因为上一章东西有点多了,我就没放进去,这一章单独拿出来,内容不多大家自己写写就可以,也算是对前面讲解的一个小练习吧。

相关注释我也加在代码上面了,大家看看代码都可以理解。

评论仓储接口和实现

   public interface ICommentRepository : IBasicRepository<Comment, Guid>
{
/// <summary>
/// 根据文章Id 获取评论
/// </summary>
/// <param name="postId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<List<Comment>> GetListOfPostAsync(Guid postId, CancellationToken cancellationToken = default);
/// <summary>
/// 根据文章Id 获取评论数量
/// </summary>
/// <param name="postId"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<int> GetCommentCountOfPostAsync(Guid postId, CancellationToken cancellationToken = default);
/// <summary>
/// 根据评论Id 下面的子获取评论
/// </summary>
/// <param name="id"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
Task<List<Comment>> GetRepliesOfComment(Guid id, CancellationToken cancellationToken = default); Task DeleteOfPost(Guid id, CancellationToken cancellationToken = default);
} public class EfCoreCommentRepository:EfCoreRepository<CoreDbContext, Comment, Guid>,ICommentRepository
{
public EfCoreCommentRepository(IDbContextProvider<CoreDbContext> dbContextProvider) : base(dbContextProvider)
{
} public async Task<List<Comment>> GetListOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(a => a.PostId == postId)
.OrderBy(a => a.CreationTime)
.ToListAsync(GetCancellationToken(cancellationToken));
} public async Task<int> GetCommentCountOfPostAsync(Guid postId, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.CountAsync(a => a.PostId == postId, GetCancellationToken(cancellationToken));
} public async Task<List<Comment>> GetRepliesOfComment(Guid id, CancellationToken cancellationToken = default)
{
return await (await GetDbSetAsync())
.Where(a => a.RepliedCommentId == id).ToListAsync(GetCancellationToken(cancellationToken));
} public async Task DeleteOfPost(Guid id, CancellationToken cancellationToken = default)
{
var recordsToDelete = await (await GetDbSetAsync()).Where(pt => pt.PostId == id).ToListAsync(GetCancellationToken(cancellationToken));
(await GetDbSetAsync()).RemoveRange(recordsToDelete);
}
}

评论接口和Dto

    public interface ICommentAppService : IApplicationService
{
Task<List<CommentWithRepliesDto>> GetHierarchicalListOfPostAsync(Guid postId); Task<CommentWithDetailsDto> CreateAsync(CreateCommentDto input); Task<CommentWithDetailsDto> UpdateAsync(Guid id, UpdateCommentDto input); Task DeleteAsync(Guid id);
} public class CommentWithRepliesDto
{
public CommentWithDetailsDto Comment { get; set; } public List<CommentWithDetailsDto> Replies { get; set; } = new List<CommentWithDetailsDto>();
} public class CommentWithDetailsDto : FullAuditedEntityDto<Guid>
{
public Guid? RepliedCommentId { get; set; } public string Text { get; set; } public BlogUserDto Writer { get; set; }
} public class CreateCommentDto
{
public Guid? RepliedCommentId { get; set; } public Guid PostId { get; set; } [Required]
public string Text { get; set; }
} public class UpdateCommentDto
{
[Required]
public string Text { get; set; }
}

接口实现


public class CommentAppService : CoreAppService, ICommentAppService
{
private IUserLookupService<IdentityUser> UserLookupService { get; } private readonly ICommentRepository _commentRepository;
private readonly IGuidGenerator _guidGenerator; public CommentAppService(ICommentRepository commentRepository, IGuidGenerator guidGenerator, IUserLookupService<IdentityUser> userLookupService)
{
_commentRepository = commentRepository;
_guidGenerator = guidGenerator;
UserLookupService = userLookupService;
} public async Task<List<CommentWithRepliesDto>> GetHierarchicalListOfPostAsync(Guid postId)
{
// 获取评论数据
var comments = await GetListOfPostAsync(postId); #region 对评论的作者进行赋值 var userDictionary = new Dictionary<Guid, BlogUserDto>(); foreach (var commentDto in comments)
{
if (commentDto.CreatorId.HasValue)
{
var creatorUser = await UserLookupService.FindByIdAsync(commentDto.CreatorId.Value); if (creatorUser != null && !userDictionary.ContainsKey(creatorUser.Id))
{
userDictionary.Add(creatorUser.Id, ObjectMapper.Map<IdentityUser, BlogUserDto>(creatorUser));
}
}
} foreach (var commentDto in comments)
{
if (commentDto.CreatorId.HasValue && userDictionary.ContainsKey((Guid)commentDto.CreatorId))
{
commentDto.Writer = userDictionary[(Guid)commentDto.CreatorId];
}
} #endregion var hierarchicalComments = new List<CommentWithRepliesDto>(); #region 包装评论数据格式 // 评论包装成2级(ps:前面的查询根据时间排序,这里不要担心子集在父级前面) foreach (var commentDto in comments)
{
var parent = hierarchicalComments.Find(c => c.Comment.Id == commentDto.RepliedCommentId); if (parent != null)
{
parent.Replies.Add(commentDto);
}
else
{
hierarchicalComments.Add(new CommentWithRepliesDto() { Comment = commentDto });
}
} hierarchicalComments = hierarchicalComments.OrderByDescending(c => c.Comment.CreationTime).ToList(); #endregion return hierarchicalComments; } public async Task<CommentWithDetailsDto> CreateAsync(CreateCommentDto input)
{
// 也可以使用这种方式(这里只是介绍用法) GuidGenerator.Create()
var comment = new Comment(_guidGenerator.Create(), input.PostId, input.RepliedCommentId, input.Text); comment = await _commentRepository.InsertAsync(comment); await CurrentUnitOfWork.SaveChangesAsync(); return ObjectMapper.Map<Comment, CommentWithDetailsDto>(comment);
} public async Task<CommentWithDetailsDto> UpdateAsync(Guid id, UpdateCommentDto input)
{
var comment = await _commentRepository.GetAsync(id); comment.SetText(input.Text); comment = await _commentRepository.UpdateAsync(comment); return ObjectMapper.Map<Comment, CommentWithDetailsDto>(comment);
} public async Task DeleteAsync(Guid id)
{
await _commentRepository.DeleteAsync(id); var replies = await _commentRepository.GetRepliesOfComment(id); foreach (var reply in replies)
{
await _commentRepository.DeleteAsync(reply.Id);
}
} private async Task<List<CommentWithDetailsDto>> GetListOfPostAsync(Guid postId)
{
var comments = await _commentRepository.GetListOfPostAsync(postId); return new List<CommentWithDetailsDto>(
ObjectMapper.Map<List<Comment>, List<CommentWithDetailsDto>>(comments));
} }

CreateMap<Comment, CommentWithDetailsDto>().Ignore(x => x.Writer);

结语

说明:

  • 1.整个评论的实现非常简单,我们只是实现了一个2层的嵌套。
  • 2.下一章我们讲授权和策略大家应该会比较喜欢,加油

联系作者:加群:867095512 @MrChuJiu

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294