Files
Yi.Admin/Yi.Abp.Net8/module/bbs/Yi.Framework.Bbs.Domain/EventHandlers/CommentCreatedEventHandler.cs

74 lines
3.4 KiB
C#
Raw Normal View History

2024-05-23 23:40:55 +08:00
using TencentCloud.Tbm.V20180129.Models;
using Volo.Abp.DependencyInjection;
2024-01-27 17:42:09 +08:00
using Volo.Abp.Domain.Entities.Events;
using Volo.Abp.EventBus;
2024-05-23 23:40:55 +08:00
using Volo.Abp.EventBus.Local;
2024-01-27 17:42:09 +08:00
using Yi.Framework.Bbs.Domain.Entities;
using Yi.Framework.Bbs.Domain.Entities.Forum;
2024-05-23 23:40:55 +08:00
using Yi.Framework.Bbs.Domain.Shared.Consts;
2024-08-13 16:45:56 +08:00
using Yi.Framework.Bbs.Domain.Shared.Enums;
2024-05-23 23:40:55 +08:00
using Yi.Framework.Bbs.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Entities;
2024-01-27 17:42:09 +08:00
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.Bbs.Domain.EventHandlers
{
/// <summary>
/// 评论创建的领域事件
/// </summary>
2024-05-22 14:35:08 +08:00
public class CommentCreatedEventHandler : ILocalEventHandler<EntityCreatedEventData<CommentAggregateRoot>>,
2024-01-27 17:42:09 +08:00
ITransientDependency
{
2024-05-23 23:40:55 +08:00
private ILocalEventBus _localEventBus;
private ISqlSugarRepository<DiscussAggregateRoot> _discussRepository;
private ISqlSugarRepository<UserAggregateRoot> _userRepository;
public CommentCreatedEventHandler(ILocalEventBus localEventBus, ISqlSugarRepository<DiscussAggregateRoot> discussRepository, ISqlSugarRepository<UserAggregateRoot> userRepository)
2024-01-27 17:42:09 +08:00
{
_userRepository = userRepository;
2024-05-23 23:40:55 +08:00
_localEventBus = localEventBus;
_discussRepository = discussRepository;
2024-01-27 17:42:09 +08:00
}
2024-05-22 14:35:08 +08:00
public async Task HandleEventAsync(EntityCreatedEventData<CommentAggregateRoot> eventData)
2024-01-27 17:42:09 +08:00
{
var commentEntity = eventData.Entity;
//给创建者发布数量+1
await _userRepository._Db.Updateable<BbsUserExtraInfoEntity>()
.SetColumns(it => it.CommentNumber == it.CommentNumber + 1)
2024-01-28 14:33:21 +08:00
.Where(it => it.UserId == commentEntity.CreatorId)
2024-01-27 17:42:09 +08:00
.ExecuteCommandAsync();
2024-05-23 23:40:55 +08:00
var disucssDto = await _discussRepository._DbQueryable
.Where(x => x.Id == commentEntity.DiscussId)
.LeftJoin<UserAggregateRoot>((dicuss, user) => dicuss.CreatorId == user.Id)
.Select((dicuss, user) =>
new
{
2024-08-07 00:01:44 +08:00
DiscussUserId = user.Id,
2024-05-23 23:40:55 +08:00
DiscussTitle = dicuss.Title,
})
.FirstAsync();
var commentUser = await _userRepository.GetFirstAsync(x => x.Id == commentEntity.CreatorId);
2024-06-08 21:51:45 +08:00
//截取30个长度
var content = commentEntity.Content.Length >= 30 ? commentEntity.Content.Substring(0, 30)+"..." : commentEntity.Content;
2024-05-23 23:40:55 +08:00
//通知主题作者,有人评论
2024-08-07 00:01:44 +08:00
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(disucssDto.DiscussUserId, string.Format(DiscussConst.CommentNotice, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
2024-08-07 11:12:01 +08:00
//如果为空,表示根路径,没有回复者
if (commentEntity.ParentId != Guid.Empty)
{
//通知回复者,有人评论
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(commentEntity.ParentId, string.Format(DiscussConst.CommentNoticeToReply, disucssDto.DiscussTitle, commentUser.UserName, content,commentEntity.DiscussId)), false);
}
2024-08-13 16:45:56 +08:00
//最后发布任务触发事件
await _localEventBus.PublishAsync(
new AssignmentEventArgs(AssignmentRequirementTypeEnum.Comment, commentUser.Id),false);
2024-08-07 11:12:01 +08:00
2024-01-27 17:42:09 +08:00
}
}
}