Skip to content

Commit

Permalink
feat: Convert deleted flag to timestamp (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex289 authored Dec 9, 2024
2 parents 4f64005 + 76da825 commit 3360b59
Show file tree
Hide file tree
Showing 23 changed files with 348 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public async Task<PagedResult<TenantViewModel>> Handle(
var tenantsQuery = _tenantRepository
.GetAllNoTracking()
.IgnoreQueryFilters()
.Include(x => x.Users.Where(y => request.IncludeDeleted || !y.Deleted))
.Where(x => request.IncludeDeleted || !x.Deleted);
.Include(x => x.Users.Where(y => request.IncludeDeleted || y.DeletedAt == null))
.Where(x => request.IncludeDeleted || x.DeletedAt == null );

if (!string.IsNullOrWhiteSpace(request.SearchTerm))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public async Task<PagedResult<UserViewModel>> Handle(
var usersQuery = _userRepository
.GetAllNoTracking()
.IgnoreQueryFilters()
.Where(x => request.IncludeDeleted || !x.Deleted);
.Where(x => request.IncludeDeleted || x.DeletedAt == null);

if (!string.IsNullOrWhiteSpace(request.SearchTerm))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public override async Task<GetTenantsByIdsResult> GetByIds(
{
Id = tenant.Id.ToString(),
Name = tenant.Name,
IsDeleted = tenant.Deleted
DeletedAt = tenant.DeletedAt == null ? "": tenant.DeletedAt.ToString()
})
.ToListAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override async Task<GetUsersByIdsResult> GetByIds(
Email = user.Email,
FirstName = user.FirstName,
LastName = user.LastName,
IsDeleted = user.Deleted
DeletedAt = user.DeletedAt == null ? "": user.DeletedAt.ToString()
})
.ToListAsync();

Expand Down
6 changes: 3 additions & 3 deletions CleanArchitecture.Domain/Entities/Entity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace CleanArchitecture.Domain.Entities;
public abstract class Entity
{
public Guid Id { get; private set; }
public bool Deleted { get; private set; }
public DateTimeOffset? DeletedAt { get; private set; }

protected Entity(Guid id)
{
Expand All @@ -24,11 +24,11 @@ public void SetId(Guid id)

public void Delete()
{
Deleted = true;
DeletedAt = DateTimeOffset.UtcNow;
}

public void Undelete()
{
Deleted = false;
DeletedAt = null;
}
}
6 changes: 3 additions & 3 deletions CleanArchitecture.Infrastructure/Database/DbContextUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ public partial class ApplicationDbContext
{
public static class DbContextUtility
{
public const string IsDeletedProperty = "Deleted";
public const string IsDeletedProperty = "DeletedAt";

public static readonly MethodInfo PropertyMethod = typeof(EF)
.GetMethod(nameof(EF.Property), BindingFlags.Static | BindingFlags.Public)
!.MakeGenericMethod(typeof(bool));
!.MakeGenericMethod(typeof(DateTimeOffset?));

public static LambdaExpression GetIsDeletedRestriction(Type type)
{
var parm = Expression.Parameter(type, "it");
var prop = Expression.Call(PropertyMethod, parm, Expression.Constant(IsDeletedProperty));
var condition = Expression.MakeBinary(ExpressionType.Equal, prop, Expression.Constant(false));
var condition = Expression.MakeBinary(ExpressionType.Equal, prop, Expression.Constant(null));
var lambda = Expression.Lambda(condition, parm);
return lambda;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace CleanArchitecture.Infrastructure.Migrations
{
/// <inheritdoc />
public partial class AddDeletedTimestamp : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTimeOffset>(
name: "DeletedAt",
table: "Users",
type: "datetimeoffset",
nullable: true);

migrationBuilder.AddColumn<DateTimeOffset>(
name: "DeletedAt",
table: "Tenants",
type: "datetimeoffset",
nullable: true);

migrationBuilder.Sql("UPDATE Users SET DeletedAt = SYSDATETIMEOFFSET() WHERE Deleted = 1");
migrationBuilder.Sql("UPDATE Tenants SET DeletedAt = SYSDATETIMEOFFSET() WHERE Deleted = 1");

migrationBuilder.DropColumn(
name: "Deleted",
table: "Users");

migrationBuilder.DropColumn(
name: "Deleted",
table: "Tenants");

migrationBuilder.UpdateData(
table: "Tenants",
keyColumn: "Id",
keyValue: new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
column: "DeletedAt",
value: null);

migrationBuilder.UpdateData(
table: "Users",
keyColumn: "Id",
keyValue: new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
column: "DeletedAt",
value: null);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "Deleted",
table: "Users",
type: "bit",
nullable: false,
defaultValue: false);

migrationBuilder.AddColumn<bool>(
name: "Deleted",
table: "Tenants",
type: "bit",
nullable: false,
defaultValue: false);

migrationBuilder.Sql("UPDATE Users SET Deleted = true WHERE DeletedAt IS NOT NULL");
migrationBuilder.Sql("UPDATE Tenants SET Deleted = true WHERE DeletedAt IS NOT NULL");

migrationBuilder.DropColumn(
name: "DeletedAt",
table: "Users");

migrationBuilder.DropColumn(
name: "DeletedAt",
table: "Tenants");

migrationBuilder.UpdateData(
table: "Tenants",
keyColumn: "Id",
keyValue: new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
column: "Deleted",
value: false);

migrationBuilder.UpdateData(
table: "Users",
keyColumn: "Id",
keyValue: new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
column: "Deleted",
value: false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.11")
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Proxies:ChangeTracking", false)
.HasAnnotation("Proxies:CheckEquality", false)
.HasAnnotation("Proxies:LazyLoading", true)
Expand All @@ -31,8 +31,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<bool>("Deleted")
.HasColumnType("bit");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetimeoffset");

b.Property<string>("Name")
.IsRequired()
Expand All @@ -47,7 +47,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
new
{
Id = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
Deleted = false,
Name = "Admin Tenant"
});
});
Expand All @@ -58,8 +57,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<bool>("Deleted")
.HasColumnType("bit");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetimeoffset");

b.Property<string>("Email")
.IsRequired()
Expand Down Expand Up @@ -103,7 +102,6 @@ protected override void BuildModel(ModelBuilder modelBuilder)
new
{
Id = new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
Deleted = false,
Email = "admin@email.com",
FirstName = "Admin",
LastName = "User",
Expand Down
Loading

0 comments on commit 3360b59

Please sign in to comment.