mirror of
https://github.com/alex289/CleanArchitecture.git
synced 2025-06-29 10:11:07 +00:00
feat: Convert deleted flag to timestamp
This commit is contained in:
parent
4f64005dee
commit
8b9253c095
@ -33,8 +33,8 @@ public sealed class GetAllTenantsQueryHandler :
|
||||
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))
|
||||
{
|
||||
|
@ -33,7 +33,7 @@ public sealed class GetAllUsersQueryHandler :
|
||||
var usersQuery = _userRepository
|
||||
.GetAllNoTracking()
|
||||
.IgnoreQueryFilters()
|
||||
.Where(x => request.IncludeDeleted || !x.Deleted);
|
||||
.Where(x => request.IncludeDeleted || x.DeletedAt == null);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(request.SearchTerm))
|
||||
{
|
||||
|
@ -40,7 +40,7 @@ public sealed class TenantsApiImplementation : TenantsApi.TenantsApiBase
|
||||
{
|
||||
Id = tenant.Id.ToString(),
|
||||
Name = tenant.Name,
|
||||
IsDeleted = tenant.Deleted
|
||||
DeletedAt = tenant.DeletedAt == null ? "": tenant.DeletedAt.ToString()
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
|
@ -42,7 +42,7 @@ public sealed class UsersApiImplementation : UsersApi.UsersApiBase
|
||||
Email = user.Email,
|
||||
FirstName = user.FirstName,
|
||||
LastName = user.LastName,
|
||||
IsDeleted = user.Deleted
|
||||
DeletedAt = user.DeletedAt == null ? "": user.DeletedAt.ToString()
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
|
@ -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)
|
||||
{
|
||||
@ -24,11 +24,11 @@ public abstract class Entity
|
||||
|
||||
public void Delete()
|
||||
{
|
||||
Deleted = true;
|
||||
DeletedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void Undelete()
|
||||
{
|
||||
Deleted = false;
|
||||
DeletedAt = null;
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
136
CleanArchitecture.Infrastructure/Migrations/20241208214605_AddDeletedTimestamp.Designer.cs
generated
Normal file
136
CleanArchitecture.Infrastructure/Migrations/20241208214605_AddDeletedTimestamp.Designer.cs
generated
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ namespace CleanArchitecture.Infrastructure.Migrations
|
||||
{
|
||||
#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)
|
||||
@ -31,8 +31,8 @@ namespace CleanArchitecture.Infrastructure.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<bool>("Deleted")
|
||||
.HasColumnType("bit");
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetimeoffset");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
@ -47,7 +47,6 @@ namespace CleanArchitecture.Infrastructure.Migrations
|
||||
new
|
||||
{
|
||||
Id = new Guid("b542bf25-134c-47a2-a0df-84ed14d03c4a"),
|
||||
Deleted = false,
|
||||
Name = "Admin Tenant"
|
||||
});
|
||||
});
|
||||
@ -58,8 +57,8 @@ namespace CleanArchitecture.Infrastructure.Migrations
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("uniqueidentifier");
|
||||
|
||||
b.Property<bool>("Deleted")
|
||||
.HasColumnType("bit");
|
||||
b.Property<DateTimeOffset?>("DeletedAt")
|
||||
.HasColumnType("datetimeoffset");
|
||||
|
||||
b.Property<string>("Email")
|
||||
.IsRequired()
|
||||
@ -103,7 +102,6 @@ namespace CleanArchitecture.Infrastructure.Migrations
|
||||
new
|
||||
{
|
||||
Id = new Guid("7e3892c0-9374-49fa-a3fd-53db637a40ae"),
|
||||
Deleted = false,
|
||||
Email = "admin@email.com",
|
||||
FirstName = "Admin",
|
||||
LastName = "User",
|
||||
|
@ -5,7 +5,7 @@ option csharp_namespace = "CleanArchitecture.Proto.Tenants";
|
||||
message Tenant {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
bool isDeleted = 3;
|
||||
optional string deletedAt = 3;
|
||||
}
|
||||
|
||||
message GetTenantsByIdsResult {
|
||||
|
@ -7,7 +7,7 @@ message GrpcUser {
|
||||
string firstName = 3;
|
||||
string lastName = 4;
|
||||
string email = 5;
|
||||
bool isDeleted = 6;
|
||||
optional string deletedAt = 6;
|
||||
}
|
||||
|
||||
message GetUsersByIdsResult {
|
||||
|
@ -4,4 +4,5 @@ namespace CleanArchitecture.Shared.Tenants;
|
||||
|
||||
public sealed record TenantViewModel(
|
||||
Guid Id,
|
||||
string Name);
|
||||
string Name,
|
||||
DateTimeOffset? DeletedAt);
|
@ -7,4 +7,4 @@ public sealed record UserViewModel(
|
||||
string Email,
|
||||
string FirstName,
|
||||
string LastName,
|
||||
bool IsDeleted);
|
||||
DateTimeOffset? DeletedAt);
|
@ -27,6 +27,7 @@ public sealed class TenantsContext : ITenantsContext
|
||||
|
||||
return result.Tenants.Select(tenant => new TenantViewModel(
|
||||
Guid.Parse(tenant.Id),
|
||||
tenant.Name));
|
||||
tenant.Name,
|
||||
string.IsNullOrWhiteSpace(tenant.DeletedAt) ? null : DateTimeOffset.Parse(tenant.DeletedAt)));
|
||||
}
|
||||
}
|
@ -30,6 +30,6 @@ public sealed class UsersContext : IUsersContext
|
||||
user.Email,
|
||||
user.FirstName,
|
||||
user.LastName,
|
||||
user.IsDeleted));
|
||||
string.IsNullOrWhiteSpace(user.DeletedAt) ? null : DateTimeOffset.Parse(user.DeletedAt)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user