summaryrefslogtreecommitdiffstats
path: root/api/WhatApi/Database/Tables/AuditTrail.cs
blob: 4ded46c9b6a87c5c31711cbc76d21d3cf6a1c95a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
namespace WhatApi.Database.Tables;

public class AuditTrail
{
    public Guid Id { get; init; }
    public Guid? UserId { get; init; }
    public required string EntityName { get; init; }
    public string? PrimaryKey { get; set; }
    public TrailType TrailType { get; set; }
    public DateTimeOffset DateUtc { get; init; }

    public Dictionary<string, object?> OldValues { get; init; } = [];
    public Dictionary<string, object?> NewValues { get; init; } = [];
    public List<string> ChangedColumns { get; init; } = [];
}

public class AuditTrailConfiguration : IEntityTypeConfiguration<AuditTrail>
{
    public void Configure(EntityTypeBuilder<AuditTrail> builder)
    {
        builder.ToTable("audit_trails");
        builder.HasIndex(e => e.EntityName);
        builder.Property(e => e.EntityName).HasMaxLength(100).IsRequired();
        builder.Property(e => e.PrimaryKey).HasMaxLength(100);
        builder.Property(e => e.DateUtc).IsRequired();
        builder.Property(e => e.TrailType).HasConversion<string>();
        builder.Property(e => e.OldValues).HasColumnType("jsonb");
        builder.Property(e => e.NewValues).HasColumnType("jsonb");
        builder.Property(e => e.ChangedColumns).HasColumnType("jsonb");
    }
}

public enum TrailType : byte
{
    None = 0,
    Create = 1,
    Update = 2,
    Delete = 3
}