45 lines
1.4 KiB
C#
45 lines
1.4 KiB
C#
using FictionArchive.Service.Shared.Contracts.Events;
|
|
using FictionArchive.Service.UserNovelDataService.Models.Database;
|
|
using FictionArchive.Service.UserNovelDataService.Services;
|
|
using MassTransit;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace FictionArchive.Service.UserNovelDataService.Consumers;
|
|
|
|
public class UserInvitedConsumer : IConsumer<IUserInvited>
|
|
{
|
|
private readonly ILogger<UserInvitedConsumer> _logger;
|
|
private readonly UserNovelDataServiceDbContext _dbContext;
|
|
|
|
public UserInvitedConsumer(
|
|
ILogger<UserInvitedConsumer> logger,
|
|
UserNovelDataServiceDbContext dbContext)
|
|
{
|
|
_logger = logger;
|
|
_dbContext = dbContext;
|
|
}
|
|
|
|
public async Task Consume(ConsumeContext<IUserInvited> context)
|
|
{
|
|
var message = context.Message;
|
|
|
|
var userId = Guid.Parse(message.InvitedUserId);
|
|
var exists = await _dbContext.Users.AnyAsync(u => u.Id == userId);
|
|
if (exists)
|
|
{
|
|
_logger.LogDebug("User {UserId} already exists, skipping", message.InvitedUserId);
|
|
return;
|
|
}
|
|
|
|
var user = new User
|
|
{
|
|
Id = userId,
|
|
OAuthProviderId = message.InvitedOAuthProviderId
|
|
};
|
|
_dbContext.Users.Add(user);
|
|
await _dbContext.SaveChangesAsync();
|
|
|
|
_logger.LogInformation("Created user stub for {UserId}", message.InvitedUserId);
|
|
}
|
|
}
|