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

148 lines
6.3 KiB
C#
Raw Normal View History

2023-12-22 16:46:39 +08:00
using Mapster;
2023-12-24 11:45:43 +08:00
using Microsoft.AspNetCore.Authorization;
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;
2025-06-02 02:12:38 +08:00
using Volo.Abp.Users;
2023-12-22 16:46:39 +08:00
using Yi.Framework.Bbs.Application.Contracts.Dtos.BbsUser;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Application.Contracts.Dtos.Comment;
using Yi.Framework.Bbs.Application.Contracts.IServices;
2024-01-11 18:51:53 +08:00
using Yi.Framework.Bbs.Domain.Entities.Forum;
2023-12-11 09:55:12 +08:00
using Yi.Framework.Bbs.Domain.Managers;
using Yi.Framework.Bbs.Domain.Shared.Consts;
using Yi.Framework.Ddd.Application;
2023-12-24 11:45:43 +08:00
using Yi.Framework.Rbac.Domain.Authorization;
2024-01-18 16:28:09 +08:00
using Yi.Framework.Rbac.Domain.Extensions;
2023-12-24 11:45:43 +08:00
using Yi.Framework.Rbac.Domain.Shared.Consts;
2023-12-11 09:55:12 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
2024-01-11 18:51:53 +08:00
namespace Yi.Framework.Bbs.Application.Services.Forum
2023-04-15 22:44:33 +08:00
{
/// <summary>
/// 评论
/// </summary>
2024-05-22 14:35:08 +08:00
public class CommentService : YiCrudAppService<CommentAggregateRoot, CommentGetOutputDto, CommentGetListOutputDto, Guid, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
2023-12-11 09:55:12 +08:00
ICommentService
2023-04-15 22:44:33 +08:00
{
2024-05-22 14:35:08 +08:00
private readonly ISqlSugarRepository<CommentAggregateRoot, Guid> _repository;
2023-12-22 16:46:39 +08:00
private readonly BbsUserManager _bbsUserManager;
2024-05-22 14:35:08 +08:00
public CommentService(ForumManager forumManager, ISqlSugarRepository<DiscussAggregateRoot> discussRepository, IDiscussService discussService, ISqlSugarRepository<CommentAggregateRoot, Guid> CommentRepository, BbsUserManager bbsUserManager) : 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-12-24 11:45:43 +08:00
_bbsUserManager = bbsUserManager;
2023-04-15 22:44:33 +08:00
}
private ForumManager _forumManager { get; set; }
2024-05-22 14:35:08 +08:00
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository { get; set; }
2023-04-15 22:44:33 +08:00
private IDiscussService _discussService { get; set; }
2025-06-02 02:12:38 +08:00
2023-04-15 22:44:33 +08:00
/// <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
{
2025-01-19 13:14:08 +08:00
await _forumManager.VerifyDiscussPermissionAsync(discussId,CurrentUser.Id);
2023-04-15 22:44:33 +08:00
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-24 13:16:18 +08:00
//该实体需要进行转换
2023-12-22 16:46:39 +08:00
//同时为所有用户id进行bbs的扩展即可
2023-12-24 13:16:18 +08:00
List<Guid> userIds = entities.Where(x => x.CreatorId != null).Select(x => x.CreatorId ?? Guid.Empty).ToList();
2023-12-24 11:45:43 +08:00
var bbsUserInfoDic = (await _bbsUserManager.GetBbsUserInfoAsync(userIds)).ToDictionary(x => x.Id);
2023-04-15 22:44:33 +08:00
2025-01-19 03:31:48 +08:00
//------数据查询完成------,以下只是dto的简单组装
2023-12-24 13:16:18 +08:00
//从根目录开始组装
//结果初始值,第一层等于全部根节点
var allOutPut = entities.OrderByDescending(x => x.CreationTime).ToList();
//获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto
List<CommentGetListOutputDto> allOutoutDto = await MapToGetListOutputDtosAsync(allOutPut);
//开始映射额外用户信息字段
allOutoutDto?.ForEach(x => x.CreateUser = bbsUserInfoDic[x.CreatorId ?? Guid.Empty].Adapt<BbsUserGetOutputDto>());
2023-12-22 16:46:39 +08:00
//开始组装dto的层级关系
2023-12-24 11:45:43 +08:00
//将全部数据进行hash
2023-12-24 13:16:18 +08:00
var dic = allOutoutDto.ToDictionary(x => x.Id);
foreach (var comment in allOutoutDto)
2023-04-15 22:44:33 +08:00
{
//不是根节点,需要赋值 被评论者用户信息等
2023-12-11 09:55:12 +08:00
if (comment.ParentId != Guid.Empty)
2023-04-15 22:44:33 +08:00
{
2023-12-16 11:40:46 +08:00
if (dic.ContainsKey(comment.ParentId))
{
var parentComment = dic[comment.ParentId];
comment.CommentedUser = parentComment.CreateUser;
}
else
{
continue;
}
2023-04-15 22:44:33 +08:00
}
//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);
}
}
//子类需要排序
2024-01-11 18:51:53 +08:00
var rootOutoutDto = allOutoutDto.Where(x => x.ParentId == Guid.Empty).ToList();
2023-12-24 13:16:18 +08:00
rootOutoutDto?.ForEach(x =>
2023-04-15 22:44:33 +08:00
{
x.Children = x.Children.OrderByDescending(x => x.CreationTime).ToList();
});
2023-12-24 13:16:18 +08:00
return new PagedResultDto<CommentGetListOutputDto>(entities.Count(), rootOutoutDto);
2023-04-15 22:44:33 +08:00
}
/// <summary>
/// 发表评论
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="UserFriendlyException"></exception>
2023-12-24 11:45:43 +08:00
[Permission("bbs:comment:add")]
[Authorize]
2023-04-15 22:44:33 +08:00
public override async Task<CommentGetOutputDto> CreateAsync(CommentCreateInputVo input)
{
2024-11-15 21:38:18 +08:00
if (string.IsNullOrWhiteSpace(input.Content)|| input.Content=="<p><br></p>")
2024-11-15 16:45:01 +08:00
{
2024-11-15 21:38:18 +08:00
throw new UserFriendlyException("评论不能为空");
2024-11-15 16:45:01 +08:00
}
2025-06-02 02:12:38 +08:00
await _bbsUserManager.VerifyUserLimitAsync(CurrentUser.GetId());
2023-12-19 13:05:03 +08:00
var discuess = await _discussRepository.GetFirstAsync(x => x.Id == input.DiscussId);
if (discuess is null)
2023-04-15 22:44:33 +08:00
{
2023-10-04 23:16:07 +08:00
throw new UserFriendlyException(DiscussConst.No_Exist);
2023-04-15 22:44:33 +08:00
}
2023-12-24 11:45:43 +08:00
//不是超级管理员,且主题开启禁止评论
2023-12-19 13:05:03 +08:00
2023-12-24 11:45:43 +08:00
if (discuess.IsDisableCreateComment == true && !CurrentUser.GetPermissions().Contains(UserConst.AdminPermissionCode))
2023-12-19 13:05:03 +08:00
{
throw new UserFriendlyException("该主题已禁止评论功能");
}
2023-12-24 11:45:43 +08:00
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);
}
}
}