Files
Yi.Admin/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Application/Services/CommentService.cs

108 lines
4.2 KiB
C#
Raw Normal View History

2023-12-11 09:55:12 +08:00
using Microsoft.AspNetCore.Mvc;
2023-04-15 22:44:33 +08:00
using SqlSugar;
2023-12-11 09:55:12 +08:00
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Yi.Framework.Bbs.Application.Contracts.Dtos.Comment;
using Yi.Framework.Bbs.Application.Contracts.IServices;
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Ddd.Application;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Application.Services
2023-04-15 22:44:33 +08:00
{
/// <summary>
/// 评论
/// </summary>
2023-12-11 09:55:12 +08:00
public class CommentService : YiCrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, Guid, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
ICommentService
2023-04-15 22:44:33 +08:00
{
2023-12-11 09:55:12 +08:00
private readonly ISqlSugarRepository<CommentEntity, Guid> _repository;
public CommentService(ForumManager forumManager, ISqlSugarRepository<DiscussEntity> discussRepository, IDiscussService discussService, ISqlSugarRepository<CommentEntity,Guid> CommentRepository) :base(CommentRepository)
2023-04-15 22:44:33 +08:00
{
_forumManager = forumManager;
_discussRepository = discussRepository;
2023-12-11 09:55:12 +08:00
_discussService = discussService;
_repository = CommentRepository;
2023-04-15 22:44:33 +08:00
}
private ForumManager _forumManager { get; set; }
2023-12-11 09:55:12 +08:00
private ISqlSugarRepository<DiscussEntity> _discussRepository { get; set; }
2023-04-15 22:44:33 +08:00
private IDiscussService _discussService { get; set; }
/// <summary>
/// 获取改主题下的评论,结构为二维列表,该查询无分页
/// </summary>
/// <param name="discussId"></param>
/// <param name="input"></param>
/// <returns></returns>
2023-12-11 09:55:12 +08:00
public async Task<PagedResultDto<CommentGetListOutputDto>> GetDiscussIdAsync([FromRoute] Guid discussId, [FromQuery] CommentGetListInputVo input)
2023-04-15 22:44:33 +08:00
{
await _discussService.VerifyDiscussPermissionAsync(discussId);
2023-12-11 09:55:12 +08:00
var entities = await _repository._DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Content), x => x.Content.Contains(input.Content))
2023-04-15 22:44:33 +08:00
.Where(x => x.DiscussId == discussId)
.Includes(x => x.CreateUser)
.ToListAsync();
//结果初始值,第一层等于全部根节点
2023-12-11 09:55:12 +08:00
var outPut = entities.Where(x => x.ParentId ==Guid.Empty).OrderByDescending(x => x.CreationTime).ToList();
2023-04-15 22:44:33 +08:00
//将全部数据进行hash
var dic = entities.ToDictionary(x => x.Id);
foreach (var comment in entities)
{
//不是根节点,需要赋值 被评论者用户信息等
2023-12-11 09:55:12 +08:00
if (comment.ParentId != Guid.Empty)
2023-04-15 22:44:33 +08:00
{
var parentComment = dic[comment.ParentId];
comment.CommentedUser = parentComment.CreateUser;
}
//root或者parent id根节点都是等于0的
var id = comment.RootId;
2023-12-11 09:55:12 +08:00
if (id != Guid.Empty)
2023-04-15 22:44:33 +08:00
{
dic[id].Children.Add(comment);
}
}
//子类需要排序
outPut.ForEach(x =>
{
x.Children = x.Children.OrderByDescending(x => x.CreationTime).ToList();
});
//获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto
List<CommentGetListOutputDto> items = await MapToGetListOutputDtosAsync(outPut);
return new PagedResultDto<CommentGetListOutputDto>(entities.Count(), items);
}
/// <summary>
/// 发表评论
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
{
if (!await _discussRepository.IsAnyAsync(x => x.Id == input.DiscussId))
{
2023-10-04 23:16:07 +08:00
throw new UserFriendlyException(DiscussConst.No_Exist);
2023-04-15 22:44:33 +08:00
}
var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.ParentId, input.RootId, input.Content);
return await MapToGetOutputDtoAsync(entity);
}
}
}