Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transaction to blocking Provider #1007

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,28 @@ public virtual async Task<T> RunInTransaction<T>(Func<Task<T>> operation)
});
}

/// <inheritdoc/>
public virtual async Task RunInTransaction(Func<Task> operation)
{
var executionStrategy = dbContext.Database.CreateExecutionStrategy();

await executionStrategy.ExecuteAsync(
async () =>
{
await using IDbContextTransaction transaction = await dbContext.Database.BeginTransactionAsync();
try
{
await operation().ConfigureAwait(false);
await transaction.CommitAsync().ConfigureAwait(false);
}
catch (Exception ex)
{
await transaction.RollbackAsync().ConfigureAwait(false);
throw;
}
});
}

/// <inheritdoc/>
public virtual async Task Delete(TEntity entity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public interface IEntityRepositoryBase<TKey, TEntity>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<T> RunInTransaction<T>(Func<Task<T>> operation);

/// <summary>
/// Runs operation in transaction without result.
/// </summary>
/// <param name="operation">Method that represents the operation.</param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Task RunInTransaction(Func<Task> operation);

/// <summary>
/// Update information about element.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,8 @@ public async Task Block_ReturnsProviderBlockDto_IfDtoIsValid()
.ReturnsAsync(provider);
providersRepositoryMock.Setup(r => r.UnitOfWork.CompleteAsync())
.ReturnsAsync(It.IsAny<int>());
providersRepositoryMock.Setup(r => r.RunInTransaction(It.IsAny<Func<Task<ProviderBlockDto>>>()))
.ReturnsAsync(providerBlockDto);

// Act
var result = await providerService.Block(providerBlockDto).ConfigureAwait(false);
Expand Down
26 changes: 15 additions & 11 deletions OutOfSchool/OutOfSchool.WebApi/Services/ProviderService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
Expand Down Expand Up @@ -303,20 +304,23 @@ public async Task<ProviderBlockDto> Block(ProviderBlockDto providerBlockDto)
// TODO: validate if current user has permission to block/unblock the provider
provider.IsBlocked = providerBlockDto.IsBlocked;
provider.BlockReason = providerBlockDto.IsBlocked ? providerBlockDto.BlockReason : null;
await providerRepository.UnitOfWork.CompleteAsync().ConfigureAwait(false);

// TODO What's happen if previous action will not successfull?
var workshops = await workshopServiceCombiner
.BlockByProvider(provider)
.ConfigureAwait(false);

foreach (var workshop in workshops)
await providerRepository.RunInTransaction(async () =>
{
logger.LogInformation($"IsBlocked property with povider Id = {provider.Id} " +
$"in workshops with Id = {workshop.Id} updated successfully.");
}
await providerRepository.UnitOfWork.CompleteAsync().ConfigureAwait(false);

var workshops = await workshopServiceCombiner
.BlockByProvider(provider)
.ConfigureAwait(false);

foreach (var workshop in workshops)
{
logger.LogInformation($"IsBlocked property with povider Id = {provider.Id} " +
$"in workshops with Id = {workshop.Id} updated successfully.");
}

logger.LogInformation($"Provider(id) {providerBlockDto.Id} IsBlocked was changed to {provider.IsBlocked}");
logger.LogInformation($"Provider(id) {providerBlockDto.Id} IsBlocked was changed to {provider.IsBlocked}");
});

return providerBlockDto;
}
Expand Down