This commit is contained in:
lumijiez
2025-06-20 14:54:19 +03:00
parent b418607a89
commit 92bdb8444b
46 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,34 @@
using AutoMapper;
using Imprink.Application.Users.Dtos;
using Imprink.Domain.Models;
using MediatR;
namespace Imprink.Application.Domains.Users;
public record SyncUserCommand(Auth0User User) : IRequest<UserDto?>;
public class SyncUserHandler(IUnitOfWork uw, IMapper mapper): IRequestHandler<SyncUserCommand, UserDto?>
{
public async Task<UserDto?> Handle(SyncUserCommand request, CancellationToken cancellationToken)
{
await uw.BeginTransactionAsync(cancellationToken);
try
{
var user = await uw.UserRepository.UpdateOrCreateUserAsync(request.User, cancellationToken);
if (user == null)
throw new Exception("User exists but could not be updated");
await uw.SaveAsync(cancellationToken);
await uw.CommitTransactionAsync(cancellationToken);
return mapper.Map<UserDto>(user);
}
catch
{
await uw.RollbackTransactionAsync(cancellationToken);
throw;
}
}
}