2023-01-26 11:25:04 +08:00
|
|
|
|
using Yi.BBS.Application.Contracts.Forum;
|
2023-02-22 19:21:21 +08:00
|
|
|
|
using Cike.AutoWebApi.Setting;
|
2023-01-26 11:25:04 +08:00
|
|
|
|
using Yi.BBS.Application.Contracts.Forum.Dtos;
|
|
|
|
|
|
using Yi.BBS.Domain.Forum.Entities;
|
|
|
|
|
|
using Yi.Framework.Ddd.Services;
|
2023-01-26 21:00:01 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
using Yi.BBS.Domain.Forum;
|
|
|
|
|
|
using Yi.Framework.Core.CurrentUsers;
|
|
|
|
|
|
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;
|
2023-03-22 19:49:20 +08:00
|
|
|
|
using SqlSugar;
|
|
|
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
using System.Collections.Generic;
|
2023-01-26 11:25:04 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.BBS.Application.Forum
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2023-03-22 19:49:20 +08:00
|
|
|
|
/// 评论
|
2023-01-26 11:25:04 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[AppService]
|
|
|
|
|
|
public class CommentService : CrudAppService<CommentEntity, CommentGetOutputDto, CommentGetListOutputDto, long, CommentGetListInputVo, CommentCreateInputVo, CommentUpdateInputVo>,
|
|
|
|
|
|
ICommentService, IAutoApiService
|
|
|
|
|
|
{
|
2023-01-26 21:00:01 +08:00
|
|
|
|
[Autowired]
|
|
|
|
|
|
private ForumManager _forumManager { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Autowired]
|
|
|
|
|
|
private ICurrentUser _currentUser { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
[Autowired]
|
|
|
|
|
|
private IRepository<DiscussEntity> _discussRepository { get; set; }
|
|
|
|
|
|
|
2023-03-26 01:28:48 +08:00
|
|
|
|
[Autowired]
|
|
|
|
|
|
private IDiscussService _discussService { get; set; }
|
2023-01-26 21:00:01 +08:00
|
|
|
|
/// <summary>
|
2023-03-22 19:49:20 +08:00
|
|
|
|
/// 获取改主题下的评论,结构为二维列表,该查询无分页
|
2023-01-26 21:00:01 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="discussId"></param>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<PagedResultDto<CommentGetListOutputDto>> GetDiscussIdAsync([FromRoute] long discussId, [FromQuery] CommentGetListInputVo input)
|
|
|
|
|
|
{
|
2023-03-26 01:28:48 +08:00
|
|
|
|
await _discussService.VerifyDiscussPermissionAsync(discussId);
|
2023-03-22 19:49:20 +08:00
|
|
|
|
|
|
|
|
|
|
var entities = await _DbQueryable.WhereIF(!string.IsNullOrEmpty(input.Content), x => x.Content.Contains(input.Content))
|
|
|
|
|
|
.Where(x => x.DiscussId == discussId)
|
2023-03-23 00:08:55 +08:00
|
|
|
|
.Includes(x=>x.CreateUser)
|
2023-03-22 19:49:20 +08:00
|
|
|
|
.ToListAsync();
|
|
|
|
|
|
|
2023-03-23 00:08:55 +08:00
|
|
|
|
//结果初始值,第一层等于全部根节点
|
2023-03-23 23:12:26 +08:00
|
|
|
|
var outPut = entities.Where(x => x.ParentId == 0).OrderByDescending(x=>x.CreationTime).ToList();
|
2023-03-22 19:49:20 +08:00
|
|
|
|
|
2023-03-23 00:08:55 +08:00
|
|
|
|
//将全部数据进行hash
|
|
|
|
|
|
var dic = entities.ToDictionary(x => x.Id);
|
2023-03-22 19:49:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
2023-03-23 00:08:55 +08:00
|
|
|
|
foreach (var comment in entities)
|
2023-03-22 19:49:20 +08:00
|
|
|
|
{
|
2023-03-23 00:08:55 +08:00
|
|
|
|
//不是根节点,需要赋值 被评论者用户信息等
|
2023-03-22 19:49:20 +08:00
|
|
|
|
if (comment.ParentId != 0)
|
|
|
|
|
|
{
|
2023-03-23 00:08:55 +08:00
|
|
|
|
var parentComment = dic[comment.ParentId];
|
|
|
|
|
|
comment.CommentedUser = parentComment.CreateUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//root或者parent id,根节点都是等于0的
|
|
|
|
|
|
var id = comment.RootId;
|
|
|
|
|
|
if (id is not 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
dic[id].Children.Add(comment);
|
2023-03-22 19:49:20 +08:00
|
|
|
|
}
|
2023-03-23 00:08:55 +08:00
|
|
|
|
|
2023-03-22 19:49:20 +08:00
|
|
|
|
}
|
2023-03-23 23:12:26 +08:00
|
|
|
|
|
|
|
|
|
|
//子类需要排序
|
|
|
|
|
|
outPut.ForEach(x =>
|
|
|
|
|
|
{
|
|
|
|
|
|
x.Children = x.Children.OrderByDescending(x => x.CreationTime).ToList();
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-03-23 00:08:55 +08:00
|
|
|
|
//获取全量主题评论, 先获取顶级的,将其他子组合到顶级下,形成一个二维,先转成dto
|
|
|
|
|
|
List<CommentGetListOutputDto>? items = await MapToGetListOutputDtosAsync(outPut);
|
2023-03-23 23:12:26 +08:00
|
|
|
|
return new PagedResultDto<CommentGetListOutputDto>(entities.Count(), items);
|
2023-01-26 21:00:01 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <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))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException(DiscussConst.主题不存在);
|
|
|
|
|
|
}
|
2023-03-23 00:08:55 +08:00
|
|
|
|
var entity = await _forumManager.CreateCommentAsync(input.DiscussId, input.ParentId,input.RootId, input.Content);
|
2023-01-26 21:00:01 +08:00
|
|
|
|
return await MapToGetOutputDtoAsync(entity);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-01-26 11:25:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|