Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/workflow-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
shell: bash
run: |
echo "🔍 Localizando Projetos de Teste..."
TEST_PROJECTS=$(find . -name "*.csproj" | grep -E "\.UnitTests|\.Tests|\.ArchitectureTests" | grep -v "Assets")
TEST_PROJECTS=$(find . -name "*.csproj" | grep -E "\.ArchitectureTests|\.IntegrationTests|\.UnitTests" | grep -v "Assets" | sort)

if [ -z "$TEST_PROJECTS" ]; then
echo "⚠️ Nenhum projeto de teste encontrado."
Expand Down
9 changes: 5 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.1" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Relational" Version="10.0.1" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="10.0.1" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="10.0.1" />
Expand All @@ -26,7 +25,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
<PackageVersion Include="Microsoft.Extensions.Options" Version="10.0.1" />
</ItemGroup>

<ItemGroup Label="System Packages">
<PackageVersion Include="AspNetCore.HealthChecks.NpgSql" Version="9.0.0" />
<PackageVersion Include="Dapper" Version="2.1.66" />
Expand All @@ -49,7 +48,7 @@
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="8.15.0" />
<PackageVersion Include="Telegram.Bot" Version="22.7.6" />
</ItemGroup>

<ItemGroup Label="Unit Tests Packages: Support">
<PackageVersion Include="Bogus" Version="35.6.5" />
<PackageVersion Include="Microsoft.AspNetCore.Mvc.Testing" Version="10.0.1" />
Expand All @@ -58,8 +57,10 @@
<PackageVersion Include="NSubstitute.Analyzers.CSharp" Version="1.0.17" />
<PackageVersion Include="Shouldly" Version="4.3.0" />
<PackageVersion Include="System.CodeDom" Version="10.0.1" />
<PackageVersion Include="Testcontainers" Version="4.9.0" />
<PackageVersion Include="Testcontainers.PostgreSql" Version="4.9.0" />
</ItemGroup>

<ItemGroup Label="Unit Tests Packages: Essentials">
<PackageVersion Update="MSTest.Analyzers" Version="4.0.2" />
<PackageVersion Update="MSTest.TestAdapter" Version="4.0.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.OpenApi;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.API.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.API")]

namespace InvoiceReminder.API.AuthenticationSetup;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
<Project Sdk="MSTest.Sdk/4.0.2">

<PropertyGroup>
<!--
Displays error on console in addition to the log file. Note that this feature comes with a performance impact.
For more information, visit https://learn.microsoft.com/dotnet/core/testing/unit-testing-platform-integration-dotnet-test#show-failure-per-test
-->
<EnableMSTestRunner>true</EnableMSTestRunner>
<OutputType>Exe</OutputType>
<UseVSTest>false</UseVSTest>
Expand Down
2 changes: 2 additions & 0 deletions InvoiceReminder.Data/Interfaces/IBaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public interface IBaseRepository<TEntity> : IDisposable where TEntity : class
{
Task<TEntity> AddAsync(TEntity entity, CancellationToken cancellationToken = default);
Task<int> BulkInsertAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default);
Task BulkRemoveAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default);
Task BulkUpdateAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default);
void Remove(TEntity entity);
Task<TEntity> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
IEnumerable<TEntity> GetAll();
Expand Down
1 change: 0 additions & 1 deletion InvoiceReminder.Data/Interfaces/IUserRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ namespace InvoiceReminder.Data.Interfaces;
public interface IUserRepository : IBaseRepository<User>
{
Task<User> GetByEmailAsync(string value, CancellationToken cancellationToken = default);
new Task<User> GetByIdAsync(Guid id, CancellationToken cancellationToken = default);
}
411 changes: 205 additions & 206 deletions InvoiceReminder.Data/Migrations/20250930210104_Initial_Create.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.Infrastructure.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.Infrastructure")]

namespace InvoiceReminder.Data.Persistence.EntitiesConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.Infrastructure.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.Infrastructure")]

namespace InvoiceReminder.Data.Persistence.EntitiesConfig;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.Infrastructure.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.Infrastructure")]

namespace InvoiceReminder.Data.Persistence.EntitiesConfig;

Expand All @@ -17,11 +17,13 @@ public void Configure(EntityTypeBuilder<JobSchedule> builder)

_ = builder.Property(x => x.Id)
.HasColumnName("id")
.HasColumnType("uuid")
.ValueGeneratedOnAdd()
.IsRequired();

_ = builder.Property(x => x.UserId)
.HasColumnName("user_id")
.HasColumnType("uuid")
.IsRequired();

_ = builder.Property(x => x.CronExpression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.Infrastructure.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.Infrastructure")]

namespace InvoiceReminder.Data.Persistence.EntitiesConfig;

Expand All @@ -23,6 +23,7 @@ public void Configure(EntityTypeBuilder<ScanEmailDefinition> builder)

_ = builder.Property(x => x.UserId)
.HasColumnName("user_id")
.HasColumnType("uuid")
.IsRequired();

_ = builder.Property(x => x.InvoiceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("InvoiceReminder.Infrastructure.UnitTests")]
[assembly: InternalsVisibleTo("InvoiceReminder.UnitTests.Infrastructure")]

namespace InvoiceReminder.Data.Persistence.EntitiesConfig;

Expand Down
15 changes: 15 additions & 0 deletions InvoiceReminder.Data/Repository/BaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ public virtual async Task<int> BulkInsertAsync(ICollection<TEntity> entities, Ca
return entities.Count;
}

public virtual async Task BulkRemoveAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default)
{
await _dbContext.BulkDeleteAsync(entities, cancellationToken: cancellationToken);
}

public virtual async Task BulkUpdateAsync(ICollection<TEntity> entities, CancellationToken cancellationToken = default)
{
foreach (var entity in entities)
{
entity.GetType().GetProperty("UpdatedAt")?.SetValue(entity, DateTime.UtcNow);
}

await _dbContext.BulkUpdateAsync(entities, cancellationToken: cancellationToken);
}

public virtual void Remove(TEntity entity)
{
if (_dbContext.Entry(entity).State == EntityState.Detached)
Expand Down
32 changes: 26 additions & 6 deletions InvoiceReminder.Data/Repository/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using InvoiceReminder.Data.Interfaces;
using InvoiceReminder.Data.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.Logging;
using System.Data;
using System.Data.Common;
Expand All @@ -14,6 +15,7 @@ public class UnitOfWork : IUnitOfWork
private readonly CoreDbContext _context;
private readonly DbConnection _connection;
private readonly ILogger<UnitOfWork> _logger;
private const string LogExceptionMessage = "{ContextualInfo} - Exception: {Message}";

public UnitOfWork(CoreDbContext context, ILogger<UnitOfWork> logger)
{
Expand All @@ -24,29 +26,47 @@ public UnitOfWork(CoreDbContext context, ILogger<UnitOfWork> logger)

public async Task SaveChangesAsync(CancellationToken cancellationToken = default)
{
await OpenConnection(cancellationToken);

using var transaction = await _connection.BeginTransactionAsync(cancellationToken);
IDbContextTransaction transaction = default;

try
{
await OpenConnection(cancellationToken);

transaction = await _context.Database.BeginTransactionAsync(cancellationToken);

_ = await _context.SaveChangesAsync(cancellationToken);

await transaction.CommitAsync(cancellationToken);
}
catch (OperationCanceledException ex) when (cancellationToken.IsCancellationRequested)
{
var method = $"{nameof(UnitOfWork)}.{nameof(SaveChangesAsync)}";
var contextualInfo = $"Method {method} execution was interrupted by a CancellationToken Request...";

if (_logger.IsEnabled(LogLevel.Warning))
{
_logger.LogWarning(ex, LogExceptionMessage, contextualInfo, ex.Message);
}

throw new OperationCanceledException(contextualInfo, ex, cancellationToken);
}
catch (Exception ex)
{
var method = $"{nameof(UnitOfWork)}.{nameof(SaveChangesAsync)}";
var contextualInfo = $"Exception raised. Rolling back changes >> {method}(...)";

if (_logger.IsEnabled(LogLevel.Error))
{
_logger.LogError(ex, "{Message}", ex.Message);
_logger.LogError(ex, LogExceptionMessage, contextualInfo, ex.Message);
}

await transaction.RollbackAsync(cancellationToken);
await transaction?.RollbackAsync(CancellationToken.None);

throw new DataLayerException($"Exception raised while saving changes: {ex.Message}", ex);
throw new DataLayerException(contextualInfo, ex);
}
Comment thread
jldsilva marked this conversation as resolved.
finally
{
transaction?.Dispose();
await CloseConnection();
}
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading