From 30329ea4dbf07bc50aabb54aa7932700c9696e21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=B7=B3?= Date: Wed, 22 Mar 2023 19:49:20 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9B=E6=B7=BB=E5=8A=A0=E8=AF=84?= =?UTF-8?q?=E8=AE=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Dtos/Comment/CommentCreateInputVo.cs | 13 ++++++ .../Dtos/Comment/CommentGetListInputVo.cs | 8 +++- .../Dtos/Comment/CommentGetListOutputDto.cs | 31 +++++++++++++- .../Forum/Dtos/Comment/CommentGetOutputDto.cs | 25 ++++++++++- .../Dtos/Comment/CommentUpdateInputVo.cs | 8 ++-- .../Forum/CommentService.cs | 42 +++++++++++++------ .../Forum/Entities/CommentEntity.cs | 36 ++++++++++++++-- .../bbs/Yi.BBS.Domain/Forum/ForumManager.cs | 5 +-- 8 files changed, 141 insertions(+), 27 deletions(-) diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentCreateInputVo.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentCreateInputVo.cs index 48386bd0..15986756 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentCreateInputVo.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentCreateInputVo.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SqlSugar; namespace Yi.BBS.Application.Contracts.Forum.Dtos { @@ -11,7 +12,19 @@ namespace Yi.BBS.Application.Contracts.Forum.Dtos /// public class CommentCreateInputVo { + public string Content { get; set; } + public long DiscussId { get; set; } + + /// + /// 根节点的评论id,这里也可根据树形查询获取到根节点,但是不够优雅,前端是二维数组,选择前端传值即可,如果是根,传0,如果不是 + /// + public long RootId { get; set; } + + /// + /// 被回复的CommentId + /// + public long ParentId { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs index 7543cb16..df908e69 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListInputVo.cs @@ -3,11 +3,17 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SqlSugar; using Yi.Framework.Ddd.Dtos; namespace Yi.BBS.Application.Contracts.Forum.Dtos { - public class CommentGetListInputVo : PagedAndSortedResultRequestDto + public class CommentGetListInputVo { + public DateTime? CreateTime { get; set; } + public string? Content { get; set; } + + //ӦѡĪѯ + public long? DiscussId { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListOutputDto.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListOutputDto.cs index d1e59e27..3a996a46 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListOutputDto.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetListOutputDto.cs @@ -3,16 +3,45 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SqlSugar; using Yi.Framework.Ddd.Dtos; +using Yi.RBAC.Application.Contracts.Identity.Dtos; namespace Yi.BBS.Application.Contracts.Forum.Dtos { + /// + /// ۶෴ + /// public class CommentGetListOutputDto : IEntityDto { public long Id { get; set; } + public DateTime? CreateTime { get; set; } public string Content { get; set; } + + /// + /// ڵid + /// + public long RootId { get; set; } + + /// + /// id + /// public long DiscussId { get; set; } - public long UserId { get; set; } + + public long ParentId { get; set; } + + + /// + /// û,ûϢ۵ûϢһڵ + /// + public UserGetOutputDto User { get; set; } + + + + /// + /// һΣǴһά飬Childrenֻһ + /// + public List Children { get; set; } = new List(); } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetOutputDto.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetOutputDto.cs index 291c642e..962aa71d 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetOutputDto.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentGetOutputDto.cs @@ -3,16 +3,39 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SqlSugar; using Yi.Framework.Ddd.Dtos; +using Yi.RBAC.Application.Contracts.Identity.Dtos; namespace Yi.BBS.Application.Contracts.Forum.Dtos { + /// + /// أصۼ + /// public class CommentGetOutputDto : IEntityDto { public long Id { get; set; } + public DateTime? CreateTime { get; set; } public string Content { get; set; } + public long DiscussId { get; set; } - public long UserId { get; set; } + + + /// + /// ûidΪû + /// + + public UserGetOutputDto User { get; set; } + /// + /// ڵid + /// + public long RootId { get; set; } + + /// + /// ظCommentId + /// + public long ParentId { get; set; } + } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentUpdateInputVo.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentUpdateInputVo.cs index 8c8d993d..24186997 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentUpdateInputVo.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application.Contracts/Forum/Dtos/Comment/CommentUpdateInputVo.cs @@ -3,15 +3,15 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using SqlSugar; namespace Yi.BBS.Application.Contracts.Forum.Dtos { public class CommentUpdateInputVo { - public long Id { get; set; } - public DateTime? CreateTime { get; set; } + public string Content { get; set; } - public long DiscussId { get; set; } - public long UserId { get; set; } + + //²ܽת } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs index 46fc1955..3fcfce99 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Application/Forum/CommentService.cs @@ -10,11 +10,16 @@ using Yi.Framework.Ddd.Repositories; using Yi.BBS.Domain.Shared.Forum.ConstClasses; using Yi.BBS.Application.Contracts.Forum.Dtos.Discuss; using Yi.Framework.Ddd.Dtos; +using SqlSugar; +using System.Security.AccessControl; +using System.Linq; +using System.Xml.Linq; +using System.Collections.Generic; namespace Yi.BBS.Application.Forum { /// - /// Comment服务实现 + /// 评论 /// [AppService] public class CommentService : CrudAppService, @@ -31,18 +36,34 @@ namespace Yi.BBS.Application.Forum /// - /// 获取改主题下的评论 + /// 获取改主题下的评论,结构为二维列表,该查询无分页 /// /// /// /// - public async Task> GetDiscussIdAsync([FromRoute] long discussId, [FromQuery] CommentGetListInputVo input) { - var entities = await _repository.GetPageListAsync(x => x.DiscussId == discussId, input); - var items = await MapToGetListOutputDtosAsync(entities); - var total = await _repository.CountAsync(x => x.IsDeleted == false); - return new PagedResultDto(total, items); + + var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Content), x => x.Content.Contains(input.Content)) + .Where(x => x.DiscussId == discussId) + .ToListAsync(); + + //获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto + List? items = await MapToGetListOutputDtosAsync(entities); + + //这里就是dto的处理啦 + + //获取根节点 + var rootDic = items.Where(x => x.ParentId != 0).ToDictionary(x => x.Id); + + foreach (var comment in items) + { + if (comment.ParentId != 0) + { + rootDic[comment.Id].Children.Add(comment); + } + } + return new PagedResultDto(0, items); } @@ -54,16 +75,11 @@ namespace Yi.BBS.Application.Forum /// public override async Task CreateAsync(CommentCreateInputVo input) { - //if (_currentUser.Id == default(long)) - //{ - // throw new UserFriendlyException("用户不存在"); - //} if (!await _discussRepository.IsAnyAsync(x => x.Id == input.DiscussId)) { throw new UserFriendlyException(DiscussConst.主题不存在); - } - var entity = await _forumManager.CreateCommentAsync(input.DiscussId, _currentUser.Id, input.Content); + var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.Content); return await MapToGetOutputDtoAsync(entity); } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs index 6fbba1b0..865920b3 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/Entities/CommentEntity.cs @@ -4,22 +4,30 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using Yi.Framework.Data.Auditing; using Yi.Framework.Data.Entities; using Yi.Framework.Ddd.Entities; +using Yi.RBAC.Domain.Identity.Entities; namespace Yi.BBS.Domain.Forum.Entities { + + /// + /// 评论表 + /// [SugarTable("Comment")] - public class CommentEntity : IEntity, ISoftDelete + public class CommentEntity : IEntity, ISoftDelete,IAuditedObject { + /// + /// 采用二维数组方式,不使用树形方式 + /// public CommentEntity() { } - internal CommentEntity(long discussId, long userId) + internal CommentEntity(long discussId) { DiscussId= discussId; - UserId = userId; } [SugarColumn(IsPrimaryKey = true)] @@ -30,6 +38,26 @@ namespace Yi.BBS.Domain.Forum.Entities public string Content { get; set; } public long DiscussId { get; set; } - public long UserId { get; set; } + + + /// + /// 根节点的评论id + /// + public long RootId { get; set; } + + /// + /// 被回复的CommentId + /// + public long ParentId { get; set; } + public DateTime CreationTime { get; set; } + + + [Navigate(NavigateType.OneToOne, nameof(CreatorId))] + public UserEntity User { get; set; } + public long? CreatorId { get; set; } + + public long? LastModifierId { get; set; } + + public DateTime? LastModificationTime { get; set; } } } diff --git a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs index fc7ce74b..270be8b4 100644 --- a/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs +++ b/Yi.Framework.Net6/src/project/bbs/Yi.BBS.Domain/Forum/ForumManager.cs @@ -40,12 +40,11 @@ namespace Yi.BBS.Domain.Forum return await _discussRepository.InsertReturnEntityAsync(entity); } - public async Task CreateCommentAsync(long discussId, long userId, string content) + public async Task CreateCommentAsync(long discussId, string content) { - var entity = new CommentEntity(discussId, userId); + var entity = new CommentEntity(discussId); entity.Id = SnowflakeHelper.NextId; entity.Content = content; - entity.CreateTime = DateTime.Now; return await _commentRepository.InsertReturnEntityAsync(entity); } }