public async Task <OperationResult <UserDataResponse> > Handle(UpdateUserDataRequest message, CancellationToken cancellationToken) { using (var context = _companyContextFactory()) using (var transaction = context.Database.BeginTransaction()) { try { var userEntity = await context.Users.SingleAsync(x => x.Id == message.Id, cancellationToken) .ConfigureAwait(false); _mapper.Map(message, userEntity); await context.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); transaction.Commit(); var response = _mapper.Map <UserDataResponse>(userEntity); return(OperationResultBuilder.CreateSuccess(response)); } catch (TaskCanceledException) { transaction.Rollback(); return(OperationResultBuilder.CreateFailure <UserDataResponse>(Resources.Exception_Update_Canceled)); } } }
public async Task <OperationResult <UserDataResponse> > Handle(RegisterCustomerRequest message, CancellationToken cancellationToken) { var customerEntity = _mapper.Map <CustomerEntity>(message); using (var context = _companyContextFactory()) using (var transaction = context.Database.BeginTransaction()) { try { var username = message.Username.ToUpper(); var isUnique = !await context.Users.AnyAsync(x => username == x.Username.ToUpper(), cancellationToken) .ConfigureAwait(false); if (!isUnique) { return(OperationResultBuilder.CreateFailure <UserDataResponse>(Resources.Exception_Registration_Username_Already_Taken)); } var userEntity = context.Users.Add(customerEntity); await context.SaveChangesAsync(cancellationToken) .ConfigureAwait(false); transaction.Commit(); var response = _mapper.Map <UserDataResponse>(userEntity); return(OperationResultBuilder.CreateSuccess(response)); } catch (TaskCanceledException) { transaction.Rollback(); return(OperationResultBuilder.CreateFailure <UserDataResponse>(Resources.Exception_Registration_Canceled)); } } }
public async Task <OperationResult <bool> > Handle(VerifyUsernameUniqueRequest message, CancellationToken cancellationToken) { using (var context = _companyContextFactory()) { var username = message.Username.ToUpper(); var isUnique = !await context.Users.AnyAsync(x => username == x.Username.ToUpper(), cancellationToken) .ConfigureAwait(false); return(OperationResultBuilder.CreateSuccess(isUnique)); } }
public async Task <OperationResult <UserDataResponse> > Handle(GetUserByCredentialsRequest message, CancellationToken cancellationToken) { using (var context = _companyContextFactory()) { var username = message.Username.ToUpper(); var userDb = await context.Users.AsNoTracking() .SingleOrDefaultAsync(x => username == x.Username.ToUpper(), cancellationToken) .ConfigureAwait(false); if (userDb == null) { return(OperationResultBuilder.CreateFailure <UserDataResponse>(Resources.Exception_Authorization_Username_Not_Found)); } if (!_passwordHasher.VerifyPasswordHash(message.Password, userDb.PasswordHash)) { return(OperationResultBuilder.CreateFailure <UserDataResponse>(Resources.Authorization_Message_Validation_Errors)); } var response = _mapper.Map <UserDataResponse>(userDb); return(OperationResultBuilder.CreateSuccess(response)); } }