Skip to content

Commit 1c5c933

Browse files
authored
Merge pull request #10 from PandaTechAM/development
Added audit trail
2 parents 0abaa60 + 42b6976 commit 1c5c933

File tree

3 files changed

+51
-4
lines changed

3 files changed

+51
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using AuditTrail.Abstractions;
2+
using Pandatech.Crypto.Helpers;
3+
4+
namespace SharedKernel.Postgres.AuditTrail;
5+
6+
public class AuditTrailDecryption : IAuditTrailDecryption
7+
{
8+
public string Decrypt(byte[]? cipherText, bool includesHash)
9+
{
10+
return includesHash
11+
? Aes256.Decrypt(cipherText ?? [])
12+
: Aes256.DecryptWithoutHash(cipherText ?? []);
13+
}
14+
}

src/SharedKernel.Postgres/Extensions/WebAppExtensions.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
using EFCore.PostgresExtensions.Extensions;
1+
using System.Reflection;
2+
using AuditTrail.Abstractions;
3+
using AuditTrail.Extensions;
4+
using EFCore.PostgresExtensions.Extensions;
25
using EntityFramework.Exceptions.PostgreSQL;
36
using Microsoft.AspNetCore.Builder;
47
using Microsoft.EntityFrameworkCore;
58
using Microsoft.Extensions.DependencyInjection;
9+
using SharedKernel.Postgres.AuditTrail;
610

711
namespace SharedKernel.Postgres.Extensions;
812

@@ -12,7 +16,7 @@ public static WebApplicationBuilder AddPostgresContext<TContext>(this WebApplica
1216
string connectionString)
1317
where TContext : DbContext
1418
{
15-
builder.Services.AddDbContextPool<TContext>((options) =>
19+
builder.Services.AddDbContextPool<TContext>(options =>
1620
{
1721
options
1822
.UseNpgsql(connectionString)
@@ -26,6 +30,33 @@ public static WebApplicationBuilder AddPostgresContext<TContext>(this WebApplica
2630
return builder;
2731
}
2832

33+
public static WebApplicationBuilder AddPostgresContextWithAuditTrail<TContext, TPermission, TConsumer>(
34+
this WebApplicationBuilder builder,
35+
string connectionString,
36+
params Assembly[] assemblies)
37+
where TContext : DbContext
38+
where TConsumer : class, IAuditTrailConsumer<TPermission, TContext>
39+
40+
{
41+
builder.Services.AddAuditTrail<TPermission, TConsumer, AuditTrailDecryption, TContext>(assemblies,
42+
s => s.AutoOpenTransaction = true);
43+
44+
45+
builder.Services.AddDbContextPool<TContext>((sp, options) =>
46+
{
47+
options
48+
.UseNpgsql(connectionString)
49+
.UseQueryLocks()
50+
.UseSnakeCaseNamingConvention()
51+
.UseExceptionProcessor()
52+
.UseAuditTrail<TPermission, TContext>(sp);
53+
});
54+
55+
builder.AddPostgresHealthCheck(connectionString);
56+
57+
return builder;
58+
}
59+
2960
public static WebApplication MigrateDatabase<TContext>(this WebApplication app)
3061
where TContext : DbContext
3162
{

src/SharedKernel.Postgres/SharedKernel.Postgres.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
<PackageReadmeFile>Readme.md</PackageReadmeFile>
99
<Authors>Pandatech</Authors>
1010
<Copyright>MIT</Copyright>
11-
<Version>1.0.6</Version>
11+
<Version>1.0.7</Version>
1212
<PackageId>Pandatech.SharedKernel.Postgres</PackageId>
1313
<Title>Pandatech.SharedKernel.Postgres</Title>
1414
<PackageTags>Pandatech; PostgreSQL; Entity Framework Core; EF Core; Health Checks; Exception Handling; Database Migration; .NET; ASP.NET Core; Shared Kernel</PackageTags>
1515
<Description>Pandatech.SharedKernel.Postgres simplifies PostgreSQL integration in ASP.NET Core applications by providing utilities for Entity Framework Core setup, health checks, and other enhancements.</Description>
1616
<RepositoryUrl>https://github.com/PandaTechAM/be-lib-sharedkernel-postgres</RepositoryUrl>
17-
<PackageReleaseNotes>Nuget updates</PackageReleaseNotes>
17+
<PackageReleaseNotes>Added AuditTrail</PackageReleaseNotes>
1818
</PropertyGroup>
1919

2020
<ItemGroup>
@@ -29,6 +29,8 @@
2929
<PackageReference Include="EFCore.NamingConventions" Version="9.0.0" />
3030
<PackageReference Include="EntityFrameworkCore.Exceptions.PostgreSQL" Version="8.1.3" />
3131
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
32+
<PackageReference Include="Pandatech.AuditTrail" Version="2.0.0" />
33+
<PackageReference Include="Pandatech.Crypto" Version="4.1.1" />
3234
<PackageReference Include="Pandatech.EFCore.AuditBase" Version="2.0.0" />
3335
<PackageReference Include="Pandatech.EFCore.PostgresExtensions" Version="4.0.0" />
3436
<PackageReference Include="PandaTech.FileExporter" Version="4.0.0" />

0 commit comments

Comments
 (0)