[FA-27] Need to test events but seems to mostly work
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
using FictionArchive.Service.Shared.Services.EventBus;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
||||
using FictionArchive.Service.UserNovelDataService.Models.IntegrationEvents;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace FictionArchive.Service.UserNovelDataService.Services.EventHandlers;
|
||||
|
||||
public class UserInvitedEventHandler : IIntegrationEventHandler<UserInvitedEvent>
|
||||
{
|
||||
private readonly UserNovelDataServiceDbContext _dbContext;
|
||||
private readonly ILogger<UserInvitedEventHandler> _logger;
|
||||
|
||||
public UserInvitedEventHandler(
|
||||
UserNovelDataServiceDbContext dbContext,
|
||||
ILogger<UserInvitedEventHandler> logger)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Handle(UserInvitedEvent @event)
|
||||
{
|
||||
var exists = await _dbContext.Users.AnyAsync(u => u.Id == @event.InvitedUserId);
|
||||
if (exists)
|
||||
{
|
||||
_logger.LogDebug("User {UserId} already exists, skipping", @event.InvitedUserId);
|
||||
return;
|
||||
}
|
||||
|
||||
var user = new User
|
||||
{
|
||||
Id = @event.InvitedUserId,
|
||||
OAuthProviderId = @event.InvitedOAuthProviderId
|
||||
};
|
||||
_dbContext.Users.Add(user);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Created user stub for {UserId}", @event.InvitedUserId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user