Redo controllers
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Imprink.Application.Exceptions;
|
||||
|
||||
public class DataUpdateException : BaseApplicationException
|
||||
{
|
||||
public DataUpdateException(string message) : base(message) { }
|
||||
public DataUpdateException(string message, Exception innerException) : base(message, innerException) { }
|
||||
}
|
||||
@@ -10,7 +10,8 @@ public class UserDto
|
||||
public required string Nickname { get; set; }
|
||||
public required string Email { get; set; }
|
||||
public bool EmailVerified { get; set; }
|
||||
public string? FullName { get; set; }
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public required bool IsActive { get; set; }
|
||||
|
||||
|
||||
48
src/Imprink.Application/Users/SetUserFullNameHandler.cs
Normal file
48
src/Imprink.Application/Users/SetUserFullNameHandler.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using Imprink.Application.Exceptions;
|
||||
using Imprink.Application.Service;
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users;
|
||||
|
||||
public record SetUserFullNameCommand(string FirstName, string LastName) : IRequest<UserDto?>;
|
||||
|
||||
public class SetUserFullNameHandler(IUnitOfWork uw, ICurrentUserService userService) : IRequestHandler<SetUserFullNameCommand, UserDto?>
|
||||
{
|
||||
public async Task<UserDto?> Handle(SetUserFullNameCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var currentUser = userService.GetCurrentUserId();
|
||||
if (currentUser == null)
|
||||
throw new NotFoundException("User token could not be accessed.");
|
||||
|
||||
var user = await uw.UserRepository.SetUserFullNameAsync(currentUser, request.FirstName, request.LastName, cancellationToken);
|
||||
if (user == null)
|
||||
throw new DataUpdateException("User name could not be updated.");
|
||||
|
||||
await uw.SaveAsync(cancellationToken);
|
||||
await uw.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Name = user.Name,
|
||||
Nickname = user.Nickname,
|
||||
Email = user.Email,
|
||||
EmailVerified = user.EmailVerified,
|
||||
FirstName = user.FirstName,
|
||||
LastName = user.LastName,
|
||||
PhoneNumber = user.PhoneNumber,
|
||||
IsActive = user.IsActive
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
await uw.RollbackTransactionAsync(cancellationToken);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,48 @@
|
||||
using Imprink.Application.Exceptions;
|
||||
using Imprink.Application.Service;
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users;
|
||||
|
||||
public record SetUserPhoneCommand(string Sub, Guid RoleId) : IRequest<UserRoleDto?>;
|
||||
public record SetUserPhoneCommand(string PhoneNumber) : IRequest<UserDto?>;
|
||||
|
||||
public class SetUserPhoneHandler
|
||||
public class SetUserPhoneHandler(IUnitOfWork uw, ICurrentUserService userService) : IRequestHandler<SetUserPhoneCommand, UserDto?>
|
||||
{
|
||||
|
||||
public async Task<UserDto?> Handle(SetUserPhoneCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
var currentUser = userService.GetCurrentUserId();
|
||||
if (currentUser == null)
|
||||
throw new NotFoundException("User token could not be accessed.");
|
||||
|
||||
var user = await uw.UserRepository.SetUserPhoneAsync(currentUser, request.PhoneNumber, cancellationToken);
|
||||
if (user == null)
|
||||
throw new DataUpdateException("User phone could not be updated.");
|
||||
|
||||
await uw.SaveAsync(cancellationToken);
|
||||
await uw.CommitTransactionAsync(cancellationToken);
|
||||
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Name = user.Name,
|
||||
Nickname = user.Nickname,
|
||||
Email = user.Email,
|
||||
EmailVerified = user.EmailVerified,
|
||||
FirstName = user.FirstName,
|
||||
LastName = user.LastName,
|
||||
PhoneNumber = user.PhoneNumber,
|
||||
IsActive = user.IsActive
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
await uw.RollbackTransactionAsync(cancellationToken);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,38 @@
|
||||
using Imprink.Application.Users.Dtos;
|
||||
using Imprink.Domain.Models;
|
||||
using MediatR;
|
||||
|
||||
namespace Imprink.Application.Users;
|
||||
|
||||
public record SyncUserCommand(Auth0User User) : IRequest<bool>;
|
||||
public record SyncUserCommand(Auth0User User) : IRequest<UserDto?>;
|
||||
|
||||
public class SyncUserHandler(IUnitOfWork uw): IRequestHandler<SyncUserCommand, bool>
|
||||
public class SyncUserHandler(IUnitOfWork uw): IRequestHandler<SyncUserCommand, UserDto?>
|
||||
{
|
||||
public async Task<bool> Handle(SyncUserCommand request, CancellationToken cancellationToken)
|
||||
public async Task<UserDto?> Handle(SyncUserCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
await uw.BeginTransactionAsync(cancellationToken);
|
||||
|
||||
try
|
||||
{
|
||||
if (!await uw.UserRepository.UpdateOrCreateUserAsync(request.User, cancellationToken))
|
||||
{
|
||||
await uw.RollbackTransactionAsync(cancellationToken);
|
||||
}
|
||||
|
||||
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 true;
|
||||
|
||||
return new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Name = user.Name,
|
||||
Nickname = user.Nickname,
|
||||
Email = user.Email,
|
||||
EmailVerified = user.EmailVerified,
|
||||
FirstName = user.FirstName,
|
||||
LastName = user.LastName,
|
||||
PhoneNumber = user.PhoneNumber,
|
||||
IsActive = user.IsActive
|
||||
};
|
||||
}
|
||||
catch
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user