feat;添加评论

This commit is contained in:
陈淳
2023-03-22 19:49:20 +08:00
parent 4b856c4905
commit 30329ea4db
8 changed files with 141 additions and 27 deletions

View File

@@ -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
/// </summary>
public class CommentCreateInputVo
{
public string Content { get; set; }
public long DiscussId { get; set; }
/// <summary>
/// 根节点的评论id这里也可根据树形查询获取到根节点但是不够优雅前端是二维数组选择前端传值即可,如果是根传0如果不是
/// </summary>
public long RootId { get; set; }
/// <summary>
/// 被回复的CommentId
/// </summary>
public long ParentId { get; set; }
}
}

View File

@@ -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; }
//Ӧ<><D3A6>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ī<EFBFBD><C4AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ
public long? DiscussId { get; set; }
}
}

View File

@@ -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
{
/// <summary>
/// <20><><EFBFBD>۶෴
/// </summary>
public class CommentGetListOutputDto : IEntityDto<long>
{
public long Id { get; set; }
public DateTime? CreateTime { get; set; }
public string Content { get; set; }
/// <summary>
/// <20><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
/// </summary>
public long RootId { get; set; }
/// <summary>
/// <20><><EFBFBD><EFBFBD>id
/// </summary>
public long DiscussId { get; set; }
public long UserId { get; set; }
public long ParentId { get; set; }
/// <summary>
/// <20>û<EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>۵<EFBFBD><DBB5>û<EFBFBD><C3BB><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public UserGetOutputDto User { get; set; }
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>Σ<EFBFBD><CEA3><EFBFBD><EFBFBD>Ǵ<EFBFBD><C7B4><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ά<EFBFBD><CEAC><EFBFBD><EFBFBD><E9A3AC>Childrenֻ<6E><D6BB>һ<EFBFBD><D2BB>
/// </summary>
public List<CommentGetListOutputDto> Children { get; set; } = new List<CommentGetListOutputDto>();
}
}

View File

@@ -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
{
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ۼ<EFBFBD><DBBC><EFBFBD>
/// </summary>
public class CommentGetOutputDto : IEntityDto<long>
{
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; }
/// <summary>
/// <20>û<EFBFBD>id<69><64><EFBFBD><EFBFBD>Ϊ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
public UserGetOutputDto User { get; set; }
/// <summary>
/// <20><><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
/// </summary>
public long RootId { get; set; }
/// <summary>
/// <20><><EFBFBD>ظ<EFBFBD><D8B8><EFBFBD>CommentId
/// </summary>
public long ParentId { get; set; }
}
}

View File

@@ -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; }
//<2F><><EFBFBD>²<EFBFBD><C2B2>ܽ<EFBFBD><DCBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>
}
}

View File

@@ -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
{
/// <summary>
/// Comment服务实现
/// 评论
/// </summary>
[AppService]
public class CommentService : CrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, long, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
@@ -31,18 +36,34 @@ namespace Yi.BBS.Application.Forum
/// <summary>
/// 获取改主题下的评论
/// 获取改主题下的评论,结构为二维列表,该查询无分页
/// </summary>
/// <param name="discussId"></param>
/// <param name="input"></param>
/// <returns></returns>
public async Task<PagedResultDto<CommentGetListOutputDto>> 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<CommentGetListOutputDto>(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<CommentGetListOutputDto>? 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<CommentGetListOutputDto>(0, items);
}
@@ -54,16 +75,11 @@ namespace Yi.BBS.Application.Forum
/// <exception cref="UserFriendlyException"></exception>
public override async Task<CommentGetOutputDto> 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);
}

View File

@@ -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
{
/// <summary>
/// 评论表
/// </summary>
[SugarTable("Comment")]
public class CommentEntity : IEntity<long>, ISoftDelete
public class CommentEntity : IEntity<long>, ISoftDelete,IAuditedObject
{
/// <summary>
/// 采用二维数组方式,不使用树形方式
/// </summary>
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; }
/// <summary>
/// 根节点的评论id
/// </summary>
public long RootId { get; set; }
/// <summary>
/// 被回复的CommentId
/// </summary>
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; }
}
}

View File

@@ -40,12 +40,11 @@ namespace Yi.BBS.Domain.Forum
return await _discussRepository.InsertReturnEntityAsync(entity);
}
public async Task<CommentEntity> CreateCommentAsync(long discussId, long userId, string content)
public async Task<CommentEntity> 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);
}
}