using GroupService.Domain.Events; using GroupService.Domain.IReposities; using GroupService.Infrastructure; using GroupService.WebApi.Application.GroupMember; using GroupService.WebApi.Application.IntegrationServices; using IM.Commons.IntegrationEvents; using MassTransit; using MediatR; namespace GroupService.WebApi.Application.EventHandler { public class GroupCreateHandler : INotificationHandler { private readonly IPublishEndpoint endpoint; private readonly IGroupMemberReposity reposity; private readonly IIdentityIntegrationService service; private readonly GroupDbContext groupDb; public GroupCreateHandler(IPublishEndpoint endpoint, IGroupMemberReposity reposity, IIdentityIntegrationService service, GroupDbContext groupDb) { this.endpoint = endpoint; this.reposity = reposity; this.service = service; this.groupDb = groupDb; } public async Task Handle(GroupCreateDomainEvent notification, CancellationToken cancellationToken) { var group = notification.Group; var userInfo = await service.FindUserByIdAsync(group.GroupMaster); string nickName = "未知昵称"; if (userInfo.Succeeded) { nickName = userInfo.Data.NickName; } reposity.Create(new Domain.Entities.GroupMember(group.GroupMaster, group.Id, userInfo.Data.NickName, userInfo.Data.Avatar, Domain.Enums.GroupMemberRole.Master)); await groupDb.SaveChangesAsync(); await endpoint.Publish(new GroupCreateEvent(group.Id, group.Name, group.GroupMaster, group.Avatar)); } } }