59 lines
2.4 KiB
C#
59 lines
2.4 KiB
C#
using ContactService.Domain;
|
|
using ContactService.Domain.Events;
|
|
using ContactService.Domain.ValueObjects;
|
|
using ContactService.Infrastructure;
|
|
using ContactService.WebApi.Application.IntegrationServices;
|
|
using IM.Commons.IntegrationEvents;
|
|
using MassTransit;
|
|
using MediatR;
|
|
|
|
namespace ContactService.WebApi.Application.EventHandler
|
|
{
|
|
public class FriendRequestStatusUpdateHandler : INotificationHandler<FriendRequestStateUpdateDomainEvent>
|
|
{
|
|
private readonly FriendDomainService friendService;
|
|
private readonly ContactDbContext contactDb;
|
|
private readonly IPublishEndpoint endpoint;
|
|
private readonly IIdentityIntegrationService idService;
|
|
|
|
public FriendRequestStatusUpdateHandler(FriendDomainService friendService, ContactDbContext contactDb, IPublishEndpoint endpoint, IIdentityIntegrationService idService)
|
|
{
|
|
this.friendService = friendService;
|
|
this.contactDb = contactDb;
|
|
this.endpoint = endpoint;
|
|
this.idService = idService;
|
|
}
|
|
|
|
public async Task Handle(FriendRequestStateUpdateDomainEvent notification, CancellationToken cancellationToken)
|
|
{
|
|
var @event = notification.Request;
|
|
if (@event.State == FriendRequestStatus.Passed)
|
|
{
|
|
var ownerInfo = await idService.FindUserByIdAsync(@event.OwnerId);
|
|
var targetInfo = await idService.FindUserByIdAsync(@event.TargetId);
|
|
|
|
if (!ownerInfo.Succeeded || !targetInfo.Succeeded)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var ownerProfile = new UserProfile(ownerInfo.Data.Id, ownerInfo.Data.NickName, ownerInfo.Data.Avatar);
|
|
var targetProfile = new UserProfile(targetInfo.Data.Id, targetInfo.Data.NickName, targetInfo.Data.Avatar);
|
|
|
|
await friendService.CreateAsync(ownerProfile, targetProfile, @event.RemarkName);
|
|
await friendService.CreateAsync(targetProfile, ownerProfile, notification.AcceptRemarkName);
|
|
await contactDb.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
await endpoint.Publish(new FriendRequestStateUpdateEvent
|
|
{
|
|
CorrelationId = @event.TargetId,
|
|
Description = @event.Description,
|
|
OwnerId = @event.OwnerId,
|
|
RemarkName = @event.RemarkName,
|
|
State = @event.State.ToString()
|
|
});
|
|
}
|
|
}
|
|
}
|