0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-06-29 18:21:08 +00:00

feat: Convert deleted flag to timestamp

This commit is contained in:
alex289 2024-12-08 22:51:08 +01:00
parent 4f64005dee
commit 8b9253c095
No known key found for this signature in database
GPG Key ID: 573F77CD2D87F863
15 changed files with 255 additions and 24 deletions

View File

@ -33,8 +33,8 @@ public sealed class GetAllTenantsQueryHandler :
var tenantsQuery = _tenantRepository var tenantsQuery = _tenantRepository
.GetAllNoTracking() .GetAllNoTracking()
.IgnoreQueryFilters() .IgnoreQueryFilters()
.Include(x => x.Users.Where(y => request.IncludeDeleted || !y.Deleted)) .Include(x => x.Users.Where(y => request.IncludeDeleted || y.DeletedAt == null))
.Where(x => request.IncludeDeleted || !x.Deleted); .Where(x => request.IncludeDeleted || x.DeletedAt == null );
if (!string.IsNullOrWhiteSpace(request.SearchTerm)) if (!string.IsNullOrWhiteSpace(request.SearchTerm))
{ {

View File

@ -33,7 +33,7 @@ public sealed class GetAllUsersQueryHandler :
var usersQuery = _userRepository var usersQuery = _userRepository
.GetAllNoTracking() .GetAllNoTracking()
.IgnoreQueryFilters() .IgnoreQueryFilters()
.Where(x => request.IncludeDeleted || !x.Deleted); .Where(x => request.IncludeDeleted || x.DeletedAt == null);
if (!string.IsNullOrWhiteSpace(request.SearchTerm)) if (!string.IsNullOrWhiteSpace(request.SearchTerm))
{ {

View File

@ -40,7 +40,7 @@ public sealed class TenantsApiImplementation : TenantsApi.TenantsApiBase
{ {
Id = tenant.Id.ToString(), Id = tenant.Id.ToString(),
Name = tenant.Name, Name = tenant.Name,
IsDeleted = tenant.Deleted DeletedAt = tenant.DeletedAt == null ? "": tenant.DeletedAt.ToString()
}) })
.ToListAsync(); .ToListAsync();

View File

@ -42,7 +42,7 @@ public sealed class UsersApiImplementation : UsersApi.UsersApiBase
Email = user.Email, Email = user.Email,
FirstName = user.FirstName, FirstName = user.FirstName,
LastName = user.LastName, LastName = user.LastName,
IsDeleted = user.Deleted DeletedAt = user.DeletedAt == null ? "": user.DeletedAt.ToString()
}) })
.ToListAsync(); .ToListAsync();

View File

@ -5,7 +5,7 @@ namespace CleanArchitecture.Domain.Entities;
public abstract class Entity public abstract class Entity
{ {
public Guid Id { get; private set; } public Guid Id { get; private set; }
public bool Deleted { get; private set; } public DateTimeOffset? DeletedAt { get; private set; }
protected Entity(Guid id) protected Entity(Guid id)
{ {
@ -24,11 +24,11 @@ public abstract class Entity
public void Delete() public void Delete()
{ {
Deleted = true; DeletedAt = DateTimeOffset.UtcNow;
} }
public void Undelete() public void Undelete()
{ {
Deleted = false; DeletedAt = null;
} }
} }

View File

@ -9,17 +9,17 @@ public partial class ApplicationDbContext
{ {
public static class DbContextUtility public static class DbContextUtility
{ {
public const string IsDeletedProperty = "Deleted"; public const string IsDeletedProperty = "DeletedAt";
public static readonly MethodInfo PropertyMethod = typeof(EF) public static readonly MethodInfo PropertyMethod = typeof(EF)
.GetMethod(nameof(EF.Property), BindingFlags.Static | BindingFlags.Public) .GetMethod(nameof(EF.Property), BindingFlags.Static | BindingFlags.Public)
!.MakeGenericMethod(typeof(bool)); !.MakeGenericMethod(typeof(DateTimeOffset?));
public static LambdaExpression GetIsDeletedRestriction(Type type) public static LambdaExpression GetIsDeletedRestriction(Type type)
{ {
var parm = Expression.Parameter(type, "it"); var parm = Expression.Parameter(type, "it");
var prop = Expression.Call(PropertyMethod, parm, Expression.Constant(IsDeletedProperty)); 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); var lambda = Expression.Lambda(condition, parm);
return lambda; return lambda;
} }

View File

@ -0,0 +1,136 @@
// <auto-generated />
using System;
using CleanArchitecture.Infrastructure.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CleanArchitecture.Infrastructure.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20241208214605_AddDeletedTimestamp")]
partial class AddDeletedTimestamp
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Proxies:ChangeTracking", false)
.HasAnnotation("Proxies:CheckEquality", false)
.HasAnnotation("Proxies:LazyLoading", true)
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("CleanArchitecture.Domain.Entities.Tenant", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetimeoffset");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("nvarchar(255)");
b.HasKey("Id");
b.ToTable("Tenants");
b.HasData(
new
{
Id = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
Name = "Admin Tenant"
});
});
modelBuilder.Entity("CleanArchitecture.Domain.Entities.User", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("datetimeoffset");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(320)
.HasColumnType("nvarchar(320)");
b.Property<string>("FirstName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<DateTimeOffset?>("LastLoggedinDate")
.HasColumnType("datetimeoffset");
b.Property<string>("LastName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<int>("Role")
.HasColumnType("int");
b.Property<int>("Status")
.HasColumnType("int");
b.Property<Guid>("TenantId")
.HasColumnType("uniqueidentifier");
b.HasKey("Id");
b.HasIndex("TenantId");
b.ToTable("Users");
b.HasData(
new
{
Id = new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
Email = "admin@email.com",
FirstName = "Admin",
LastName = "User",
Password = "$2a$12$Blal/uiFIJdYsCLTMUik/egLbfg3XhbnxBC6Sb5IKz2ZYhiU/MzL2",
Role = 0,
Status = 0,
TenantId = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a")
});
});
modelBuilder.Entity("CleanArchitecture.Domain.Entities.User", b =>
{
b.HasOne("CleanArchitecture.Domain.Entities.Tenant", "Tenant")
.WithMany("Users")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Tenant");
});
modelBuilder.Entity("CleanArchitecture.Domain.Entities.Tenant", b =>
{
b.Navigation("Users");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -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);
}
}
}

View File

@ -17,7 +17,7 @@ namespace CleanArchitecture.Infrastructure.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "7.0.11") .HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Proxies:ChangeTracking", false) .HasAnnotation("Proxies:ChangeTracking", false)
.HasAnnotation("Proxies:CheckEquality", false) .HasAnnotation("Proxies:CheckEquality", false)
.HasAnnotation("Proxies:LazyLoading", true) .HasAnnotation("Proxies:LazyLoading", true)
@ -31,8 +31,8 @@ namespace CleanArchitecture.Infrastructure.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<bool>("Deleted") b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("bit"); .HasColumnType("datetimeoffset");
b.Property<string>("Name") b.Property<string>("Name")
.IsRequired() .IsRequired()
@ -47,7 +47,6 @@ namespace CleanArchitecture.Infrastructure.Migrations
new new
{ {
Id = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"), Id = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
Deleted = false,
Name = "Admin Tenant" Name = "Admin Tenant"
}); });
}); });
@ -58,8 +57,8 @@ namespace CleanArchitecture.Infrastructure.Migrations
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");
b.Property<bool>("Deleted") b.Property<DateTimeOffset?>("DeletedAt")
.HasColumnType("bit"); .HasColumnType("datetimeoffset");
b.Property<string>("Email") b.Property<string>("Email")
.IsRequired() .IsRequired()
@ -103,7 +102,6 @@ namespace CleanArchitecture.Infrastructure.Migrations
new new
{ {
Id = new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"), Id = new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
Deleted = false,
Email = "admin@email.com", Email = "admin@email.com",
FirstName = "Admin", FirstName = "Admin",
LastName = "User", LastName = "User",

View File

@ -5,7 +5,7 @@ option csharp_namespace = "CleanArchitecture.Proto.Tenants";
message Tenant { message Tenant {
string id = 1; string id = 1;
string name = 2; string name = 2;
bool isDeleted = 3; optional string deletedAt = 3;
} }
message GetTenantsByIdsResult { message GetTenantsByIdsResult {

View File

@ -7,7 +7,7 @@ message GrpcUser {
string firstName = 3; string firstName = 3;
string lastName = 4; string lastName = 4;
string email = 5; string email = 5;
bool isDeleted = 6; optional string deletedAt = 6;
} }
message GetUsersByIdsResult { message GetUsersByIdsResult {

View File

@ -4,4 +4,5 @@ namespace CleanArchitecture.Shared.Tenants;
public sealed record TenantViewModel( public sealed record TenantViewModel(
Guid Id, Guid Id,
string Name); string Name,
DateTimeOffset? DeletedAt);

View File

@ -7,4 +7,4 @@ public sealed record UserViewModel(
string Email, string Email,
string FirstName, string FirstName,
string LastName, string LastName,
bool IsDeleted); DateTimeOffset? DeletedAt);

View File

@ -27,6 +27,7 @@ public sealed class TenantsContext : ITenantsContext
return result.Tenants.Select(tenant => new TenantViewModel( return result.Tenants.Select(tenant => new TenantViewModel(
Guid.Parse(tenant.Id), Guid.Parse(tenant.Id),
tenant.Name)); tenant.Name,
string.IsNullOrWhiteSpace(tenant.DeletedAt) ? null : DateTimeOffset.Parse(tenant.DeletedAt)));
} }
} }

View File

@ -30,6 +30,6 @@ public sealed class UsersContext : IUsersContext
user.Email, user.Email,
user.FirstName, user.FirstName,
user.LastName, user.LastName,
user.IsDeleted)); string.IsNullOrWhiteSpace(user.DeletedAt) ? null : DateTimeOffset.Parse(user.DeletedAt)));
} }
} }