API oldaláról sokminden
This commit is contained in:
@@ -1,27 +1,109 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.Common.RequestContext;
|
||||
using WorkFlowCheck.DL.Configurations;
|
||||
using WorkFlowCheck.DL.Extensions;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL
|
||||
{
|
||||
public class AppDbContext : DbContext
|
||||
{
|
||||
private readonly IRequestContext _requestContext;
|
||||
|
||||
#if !DEBUG
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options, IRequestContext requestContext)
|
||||
: base(options)
|
||||
{
|
||||
_requestContext = requestContext;
|
||||
}
|
||||
#endif
|
||||
#if DEBUG
|
||||
public AppDbContext(DbContextOptions<AppDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
var _requestContext = new { CurrentUserEmail = "", CurrentUsername = "", TraceId = 0 };
|
||||
}
|
||||
|
||||
public DbSet<Entities.User> Users { get; set; } = null!;
|
||||
#endif
|
||||
#region Entities DbSet
|
||||
public DbSet<Entities.CheckListHeader> CheckListHeaders { get; set; } = null!;
|
||||
public DbSet<Entities.CheckListTemplateHeader> CheckListTemplateHeaders { get; set; } = null!;
|
||||
public DbSet<Entities.CheckListTemplateRow> CheckListTemplateRows { get; set; } = null!;
|
||||
public DbSet<Entities.CheckPoint> CheckPoints { get; set; } = null!;
|
||||
public DbSet<Entities.Equipment> Equipments { get; set; } = null!;
|
||||
public DbSet<Entities.Location> Locations { get; set; } = null!;
|
||||
public DbSet<Entities.Role> Roles { get; set; } = null!;
|
||||
public DbSet<Entities.User> Users { get; set; } = null!;
|
||||
public DbSet<Entities.UsersCheckListTemplateHeader> UsersCheckListTemplateHeaders { get; set; } = null!;
|
||||
#endregion
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfiguration(new EquipmentTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new LocationTypeConfiguration());
|
||||
modelBuilder.ApplyConfiguration(new UserTypeConfiguration());
|
||||
|
||||
modelBuilder.RegisterSoftDeleteQueryFilters();
|
||||
}
|
||||
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
SaveChangesCore();
|
||||
return await base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
protected virtual void SaveChangesCore()
|
||||
{
|
||||
var utcNow = DateTime.UtcNow;
|
||||
|
||||
var deletedEntries = ChangeTracker
|
||||
.Entries()
|
||||
.Where(e => e.Entity is ISoftDeletableEntity && e.State == EntityState.Deleted);
|
||||
|
||||
UpdateDeletedEntries(deletedEntries);
|
||||
|
||||
var modifiedEntries = ChangeTracker
|
||||
.Entries()
|
||||
.Where(e => e.Entity is IAuditableEntity && e.State == EntityState.Modified);
|
||||
|
||||
UpdateModifiedEntires(modifiedEntries, utcNow);
|
||||
|
||||
var addedEntries = ChangeTracker
|
||||
.Entries()
|
||||
.Where(e => e.Entity is IAuditableEntity && e.State == EntityState.Added);
|
||||
|
||||
UpdateAddedEntries(addedEntries, utcNow);
|
||||
}
|
||||
private void UpdateAddedEntries(IEnumerable<EntityEntry> addedEntries, DateTime utcNow)
|
||||
{
|
||||
foreach (var entry in addedEntries)
|
||||
{
|
||||
((IAuditableEntity)entry.Entity).CreatedAt = utcNow;
|
||||
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
||||
((IAuditableEntity)entry.Entity).CreatedBy = _requestContext.CurrentUsername;
|
||||
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername;
|
||||
}
|
||||
}
|
||||
private void UpdateModifiedEntires(IEnumerable<EntityEntry> modifiedEntries, DateTime utcNow)
|
||||
{
|
||||
foreach (var entry in modifiedEntries)
|
||||
{
|
||||
((IAuditableEntity)entry.Entity).LastModAt = utcNow;
|
||||
((IAuditableEntity)entry.Entity).LastModBy = _requestContext.CurrentUsername;
|
||||
}
|
||||
}
|
||||
private void UpdateDeletedEntries(IEnumerable<EntityEntry> deletedEntries)
|
||||
{
|
||||
foreach (var entry in deletedEntries)
|
||||
{
|
||||
((ISoftDeletableEntity)entry.Entity).IsDeleted = true;
|
||||
entry.State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.DL.Configurations
|
||||
{
|
||||
public class EquipmentTypeConfiguration : IEntityTypeConfiguration<Entities.Equipment>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Entities.Equipment> builder)
|
||||
{
|
||||
builder.ToTable("Equipment");
|
||||
builder.HasKey(e => e.Id);
|
||||
builder.Property(e => e.Id).ValueGeneratedOnAdd();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.DL.Configurations
|
||||
{
|
||||
public class LocationTypeConfiguration : IEntityTypeConfiguration<Entities.Location>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Entities.Location> builder)
|
||||
{
|
||||
builder.ToTable("Location");
|
||||
builder.HasKey(e => e.Id);
|
||||
builder.Property(e => e.Id).ValueGeneratedOnAdd();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.DL.Configurations
|
||||
{
|
||||
public class RoleTypeConfiguration : IEntityTypeConfiguration<Entities.Role>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Entities.Role> builder)
|
||||
{
|
||||
builder.ToTable("Roles");
|
||||
builder.HasKey(e => e.Id);
|
||||
builder.Property(e => e.Id).ValueGeneratedOnAdd();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class CheckListHeader : IAuditableEntity, ISoftDeletableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShortName { get; set; } = null!;
|
||||
public string Description { get; set; } = null!;
|
||||
public string DocumentNumber { get; set; } = null!;
|
||||
public bool IsStorno { get; set; }
|
||||
public DateTime LastModAt { get; set; }
|
||||
public string LastModBy { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
public virtual ICollection<CheckListTemplateRow> CheckListTemplateRows { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,17 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class CheckListTemplateHeader
|
||||
public class CheckListTemplateHeader:ISoftDeletableEntity,IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShortName { get; set; } = null!;
|
||||
public string Description { get; set; } = null!;
|
||||
public bool IsDeleted { get ; set ; }
|
||||
public DateTime LastModAt { get ; set ; }
|
||||
public string LastModBy { get ; set ; }
|
||||
public DateTime CreatedAt { get ; set ; }
|
||||
public string CreatedBy { get ; set ; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class CheckListTemplateRow : ISoftDeletableEntity, IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int RowIndex { get; set; }
|
||||
public int CheckListTemplateHeaderId { get; set; }
|
||||
public virtual CheckListTemplateHeader CheckListTemplateHeader { get; set; }
|
||||
public int EquipmentId { get; set; }
|
||||
public virtual Equipment Equipment { get; set; }
|
||||
public int CheckPointId { get; set; }
|
||||
public virtual CheckPoint EquipmentPoint { get; set; }
|
||||
public string OperationDescription { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
|
||||
public DateTime LastModAt { get; set; }
|
||||
public string LastModBy { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class CheckPoint : ISoftDeletableEntity, IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public string Code { get; set; }
|
||||
public bool IsDeleted { get; set; }
|
||||
public DateTime LastModAt { get; set; }
|
||||
public string LastModBy { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class Equipment :ISoftDeletableEntity,IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string ShortName { get; set; }
|
||||
public string EquipmentNumber { get; set; }
|
||||
public bool IsDeleted { get ; set ; }
|
||||
public DateTime LastModAt { get ; set ; }
|
||||
public string LastModBy { get ; set ; }
|
||||
public DateTime CreatedAt { get ; set ; }
|
||||
public string CreatedBy { get ; set ; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class Location : ISoftDeletableEntity, IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string FullName { get; set; } = null!;
|
||||
public bool IsDeleted { get ; set ; }
|
||||
public DateTime LastModAt { get ; set ; }
|
||||
public string LastModBy { get ; set ; }
|
||||
public DateTime CreatedAt { get ; set ; }
|
||||
public string CreatedBy { get ; set ; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class Role :ISoftDeletableEntity,IAuditableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string RoleName { get; set; }
|
||||
public bool IsDeleted { get ; set ; }
|
||||
public DateTime LastModAt { get ; set ; }
|
||||
public string LastModBy { get ; set ; }
|
||||
public DateTime CreatedAt { get ; set ; }
|
||||
public string CreatedBy { get ; set ; }
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public class User
|
||||
public class User:IAuditableEntity,ISoftDeletableEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Email { get; set; } = null!;
|
||||
@@ -10,5 +11,10 @@ namespace WorkFlowCheck.DL.Entities
|
||||
public string LastName { get; set; } = null!;
|
||||
public string UserName { get; set; } = null!;
|
||||
public string? PasswordHash { get; set; } = null;
|
||||
public DateTime LastModAt { get ; set ; }
|
||||
public string LastModBy { get ; set ; }
|
||||
public DateTime CreatedAt { get ; set ; }
|
||||
public string CreatedBy { get ; set ; }
|
||||
public bool IsDeleted { get ; set ; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,9 @@ namespace WorkFlowCheck.DL.Entities
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int UsersId { get; set; }
|
||||
public int CheckListTemplateHeader { get; set; }
|
||||
public virtual User User { get; set; }
|
||||
public int CheckListTemplateHeaderId { get; set; }
|
||||
public virtual CheckListTemplateHeader CheckListTemplateHeader { get; set; }
|
||||
public bool Enabled { get; set; } = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using WorkFlowCheck.DL.Interfaces;
|
||||
|
||||
namespace WorkFlowCheck.DL.Extensions
|
||||
{
|
||||
public static class DbContextExtensions
|
||||
{
|
||||
public static void RegisterSoftDeleteQueryFilters(this ModelBuilder modelBuilder)
|
||||
{
|
||||
foreach (var entityType in modelBuilder.Model.GetEntityTypes().Where(e => typeof(ISoftDeletableEntity).IsAssignableFrom(e.ClrType)))
|
||||
{
|
||||
var parameter = Expression.Parameter(entityType.ClrType);
|
||||
|
||||
var softDeletableProperty = Expression.Property(parameter, nameof(ISoftDeletableEntity.IsDeleted));
|
||||
var compareExpression = Expression.MakeBinary(ExpressionType.Equal, softDeletableProperty, Expression.Constant(false));
|
||||
var lambda = Expression.Lambda(compareExpression, parameter);
|
||||
|
||||
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.DL.Interfaces
|
||||
{
|
||||
public interface IAuditableEntity
|
||||
{
|
||||
public DateTime LastModAt { get; set; }
|
||||
public string LastModBy { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string CreatedBy { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace WorkFlowCheck.DL.Interfaces
|
||||
{
|
||||
public interface ISoftDeletableEntity
|
||||
{
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,9 +7,9 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.12" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.12" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.12">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
|
||||
Reference in New Issue
Block a user