Skip to content

Commit

Permalink
feat: auto set date created and date updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Thundernerd committed Feb 27, 2024
1 parent c1ed412 commit beccd99
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 14 deletions.
54 changes: 54 additions & 0 deletions GTRContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using TNRD.Zeepkist.GTR.Database.Models;
using Version = TNRD.Zeepkist.GTR.Database.Models.Version;

Expand Down Expand Up @@ -166,5 +167,58 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
OnModelCreatingPartial(modelBuilder);
}

public override int SaveChanges()
{
SetDateCreated();
SetDateUpdated();
return base.SaveChanges();
}

public override int SaveChanges(bool acceptAllChangesOnSuccess)
{
SetDateCreated();
SetDateUpdated();
return base.SaveChanges(acceptAllChangesOnSuccess);
}

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new())
{
SetDateCreated();
SetDateUpdated();
return base.SaveChangesAsync(cancellationToken);
}

public override Task<int> SaveChangesAsync(
bool acceptAllChangesOnSuccess,
CancellationToken cancellationToken = new()
)
{
SetDateCreated();
SetDateUpdated();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}

private void SetDateCreated()
{
IEnumerable<EntityEntry<ModelBase>> entries =
ChangeTracker.Entries<ModelBase>().Where(x => x.State == EntityState.Added);

foreach (EntityEntry<ModelBase> entry in entries)
{
entry.Entity.DateCreated = DateTime.UtcNow;
}
}

private void SetDateUpdated()
{
IEnumerable<EntityEntry<ModelBase>> entries =
ChangeTracker.Entries<ModelBase>().Where(x => x.State == EntityState.Modified);

foreach (EntityEntry<ModelBase> entry in entries)
{
entry.Entity.DateUpdated = DateTime.UtcNow;
}
}

partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
10 changes: 2 additions & 8 deletions ModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,9 @@ public class ModelBase : IIdentifiable<int>
[Attr]
public int Id { get; set; }

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Column("date_created")]
[Attr]
public DateTime DateCreated { get; set; }
[Column("date_created")] [Attr] public DateTime DateCreated { get; set; }

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
[Column("date_updated")]
[Attr]
public DateTime DateUpdated { get; set; }
[Column("date_updated")] [Attr] public DateTime DateUpdated { get; set; }

string? IIdentifiable.StringId
{
Expand Down
12 changes: 6 additions & 6 deletions Zeepkist.GTR.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
<Nullable>enable</Nullable>
<AssemblyName>TNRD.Zeepkist.GTR.Database</AssemblyName>
<RootNamespace>TNRD.Zeepkist.GTR.Database</RootNamespace>
<Version>3.0.1</Version>
<Version>3.1.0</Version>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonApiDotNetCore" Version="5.5.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.12"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.12">
<PackageReference Include="JsonApiDotNetCore" Version="5.*"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.*"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.*">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.11"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.*"/>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.*"/>
</ItemGroup>

</Project>

0 comments on commit beccd99

Please sign in to comment.