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

90 lines
4.2 KiB
C#
Raw Normal View History

2024-01-27 17:42:09 +08:00
using Volo.Abp.DependencyInjection;
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;
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>
public class AgreeCreatedEventHandler : ILocalEventHandler<EntityCreatedEventData<AgreeEntity>>,
ITransientDependency
{
2024-05-23 23:40:55 +08:00
private ISqlSugarRepository<UserAggregateRoot> _userRepository;
private ISqlSugarRepository<BbsUserExtraInfoEntity> _userInfoRepository;
2024-01-27 17:42:09 +08:00
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
2024-05-23 23:40:55 +08:00
private ILocalEventBus _localEventBus;
public AgreeCreatedEventHandler(ISqlSugarRepository<BbsUserExtraInfoEntity> userInfoRepository, ISqlSugarRepository<AgreeEntity> agreeRepository, ILocalEventBus localEventBus, ISqlSugarRepository<UserAggregateRoot> userRepository)
2024-01-27 17:42:09 +08:00
{
2024-05-23 23:40:55 +08:00
_userInfoRepository = userInfoRepository;
2024-01-27 17:42:09 +08:00
_agreeRepository = agreeRepository;
2024-05-23 23:40:55 +08:00
_localEventBus = localEventBus;
_userRepository = userRepository;
2024-01-27 17:42:09 +08:00
}
public async Task HandleEventAsync(EntityCreatedEventData<AgreeEntity> eventData)
{
var agreeEntity = eventData.Entity;
2024-05-23 23:40:55 +08:00
//查询主题的信息
var discussAndAgreeDto = await _agreeRepository._DbQueryable
.LeftJoin<DiscussAggregateRoot>((agree, discuss) => agree.DiscussId == discuss.Id)
.Select((agree, discuss) =>
new
{
2024-08-07 00:01:44 +08:00
DiscussId=discuss.Id,
2024-05-23 23:40:55 +08:00
DiscussTitle = discuss.Title,
DiscussCreatorId = discuss.CreatorId,
})
.FirstAsync();
//查询点赞者用户
var agreeUser = await _userRepository.GetFirstAsync(x => x.Id == agreeEntity.CreatorId);
//给创建者点赞数量+1
await _userInfoRepository._Db.Updateable<BbsUserExtraInfoEntity>()
2024-05-22 22:16:44 +08:00
.SetColumns(it => it.AgreeNumber == it.AgreeNumber + 1)
2024-05-23 23:40:55 +08:00
.Where(it => it.UserId == discussAndAgreeDto.DiscussCreatorId)
2024-01-27 17:42:09 +08:00
.ExecuteCommandAsync();
2024-05-23 23:40:55 +08:00
//通知主题作者,有人点赞
2024-08-07 00:01:44 +08:00
await _localEventBus.PublishAsync(new BbsNoticeEventArgs(discussAndAgreeDto.DiscussCreatorId!.Value, string.Format(DiscussConst.AgreeNotice, discussAndAgreeDto.DiscussTitle, agreeUser.UserName,discussAndAgreeDto.DiscussId)), false);
2024-05-23 23:40:55 +08:00
2024-01-27 17:42:09 +08:00
}
}
/// <summary>
/// 取消点赞
/// </summary>
2024-05-23 23:40:55 +08:00
public class AgreeDeletedEventHandler : ILocalEventHandler<EntityDeletedEventData<AgreeEntity>>,
2024-01-27 17:42:09 +08:00
ITransientDependency
{
private ISqlSugarRepository<BbsUserExtraInfoEntity> _userRepository;
private ISqlSugarRepository<AgreeEntity> _agreeRepository;
2024-05-23 23:40:55 +08:00
private ILocalEventBus _localEventBus;
public AgreeDeletedEventHandler(ISqlSugarRepository<BbsUserExtraInfoEntity> userRepository, ISqlSugarRepository<AgreeEntity> agreeRepository, ILocalEventBus localEventBus)
2024-01-27 17:42:09 +08:00
{
_userRepository = userRepository;
_agreeRepository = agreeRepository;
2024-05-23 23:40:55 +08:00
_localEventBus = localEventBus;
2024-01-27 17:42:09 +08:00
}
2024-05-23 23:40:55 +08:00
public async Task HandleEventAsync(EntityDeletedEventData<AgreeEntity> eventData)
2024-01-27 17:42:09 +08:00
{
var agreeEntity = eventData.Entity;
2024-05-22 14:35:08 +08:00
var userId = await _agreeRepository._DbQueryable.LeftJoin<DiscussAggregateRoot>((agree, discuss) => agree.DiscussId == discuss.Id)
2024-01-27 17:42:09 +08:00
.Select((agree, discuss) => discuss.CreatorId).FirstAsync();
2024-05-23 23:40:55 +08:00
//给创建者点赞数量-1
2024-01-27 17:42:09 +08:00
await _userRepository._Db.Updateable<BbsUserExtraInfoEntity>()
2024-05-23 23:40:55 +08:00
.SetColumns(it => it.AgreeNumber == it.AgreeNumber - 1)
2024-01-28 14:33:21 +08:00
.Where(it => it.UserId == userId)
2024-01-27 17:42:09 +08:00
.ExecuteCommandAsync();
}
}
}