diff --git a/DbContexts/PITDbContext.cs b/DbContexts/PITDbContext.cs index 6932e8e..75d935d 100644 --- a/DbContexts/PITDbContext.cs +++ b/DbContexts/PITDbContext.cs @@ -7,6 +7,7 @@ namespace DTPersonalInfoTracker.DbContexts; public class PITDbContext : DbContext { public DbSet Personels { get; set; } + public DbSet Logs { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { @@ -16,5 +17,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity().HasKey(p => p.RecordId); + modelBuilder.Entity().HasKey(p => p.Id); + modelBuilder.Entity().Property(p => p.Id).ValueGeneratedOnAdd(); } } \ No newline at end of file diff --git a/Helpers/DbHelper.cs b/Helpers/DbHelper.cs index 9ff9fc9..8e55b40 100644 --- a/Helpers/DbHelper.cs +++ b/Helpers/DbHelper.cs @@ -45,6 +45,7 @@ public static void AddFieldListsToDb(IList personelModels) context.Personels.AddRange(personelModels); context.SaveChanges(); LoggerHelper.Information("Personel list successfully added to database."); + LoggerHelper.LogToDb(context, new LogModel {Date = DateTime.Now, OperationName = "Update", Message = "Personel list successfully added to database"}); } public static void InitializeDb(PITDbContext context) @@ -60,5 +61,6 @@ public static void TruncateTable(PITDbContext context) { context.Personels.RemoveRange(context.Personels); LoggerHelper.Information("Personel table truncated."); + LoggerHelper.LogToDb(context, new LogModel {Date = DateTime.Now, OperationName = "Truncate", Message = "Personel table truncated."}); } } \ No newline at end of file diff --git a/Helpers/LoggerHelper.cs b/Helpers/LoggerHelper.cs index 2279989..8b0afb8 100644 --- a/Helpers/LoggerHelper.cs +++ b/Helpers/LoggerHelper.cs @@ -1,4 +1,6 @@ -using Serilog; +using DTPersonalInfoTracker.DbContexts; +using DTPersonalInfoTracker.Models; +using Serilog; using Serilog.Core; namespace DTPersonalInfoTracker.Helpers; @@ -33,4 +35,10 @@ public static void Information(string message) { DTLogger.Information(message); } + + public static void LogToDb(PITDbContext context, LogModel log) + { + context.Logs.Add(log); + context.SaveChanges(); + } } \ No newline at end of file diff --git a/Migrations/20220810062226_LogLayerAdded.Designer.cs b/Migrations/20220810062226_LogLayerAdded.Designer.cs new file mode 100644 index 0000000..03bbc4c --- /dev/null +++ b/Migrations/20220810062226_LogLayerAdded.Designer.cs @@ -0,0 +1,96 @@ +// +using System; +using DTPersonalInfoTracker.DbContexts; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace DTPersonalInfoTracker.Migrations +{ + [DbContext(typeof(PITDbContext))] + [Migration("20220810062226_LogLayerAdded")] + partial class LogLayerAdded + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.7") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + + modelBuilder.Entity("DTPersonalInfoTracker.Models.LogModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Date") + .HasColumnType("datetime2"); + + b.Property("Message") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OperationName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Logs"); + }); + + modelBuilder.Entity("DTPersonalInfoTracker.Models.PersonelModel", b => + { + b.Property("RecordId") + .HasColumnType("nvarchar(450)"); + + b.Property("Ad") + .HasColumnType("nvarchar(max)"); + + b.Property("Birim") + .HasColumnType("nvarchar(max)"); + + b.Property("Bolum") + .HasColumnType("nvarchar(max)"); + + b.Property("CepNumarasi") + .HasColumnType("nvarchar(max)"); + + b.Property("Grup") + .HasColumnType("nvarchar(max)"); + + b.Property("Pozisyon") + .HasColumnType("nvarchar(max)"); + + b.Property("PozisyonSeviyesi") + .HasColumnType("nvarchar(max)"); + + b.Property("SicilNo") + .HasColumnType("nvarchar(max)"); + + b.Property("SirketEmail") + .HasColumnType("nvarchar(max)"); + + b.Property("Soyad") + .HasColumnType("nvarchar(max)"); + + b.Property("Yonetici") + .HasColumnType("nvarchar(max)"); + + b.HasKey("RecordId"); + + b.ToTable("Personels"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Migrations/20220810062226_LogLayerAdded.cs b/Migrations/20220810062226_LogLayerAdded.cs new file mode 100644 index 0000000..4518f4a --- /dev/null +++ b/Migrations/20220810062226_LogLayerAdded.cs @@ -0,0 +1,34 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DTPersonalInfoTracker.Migrations +{ + public partial class LogLayerAdded : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Logs", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + Message = table.Column(type: "nvarchar(max)", nullable: false), + OperationName = table.Column(type: "nvarchar(max)", nullable: false), + Date = table.Column(type: "datetime2", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Logs", x => x.Id); + }); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Logs"); + } + } +} diff --git a/Migrations/PITDbContextModelSnapshot.cs b/Migrations/PITDbContextModelSnapshot.cs index dbf5500..66cdcf8 100644 --- a/Migrations/PITDbContextModelSnapshot.cs +++ b/Migrations/PITDbContextModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using DTPersonalInfoTracker.DbContexts; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -21,6 +22,30 @@ protected override void BuildModel(ModelBuilder modelBuilder) SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder, 1L, 1); + modelBuilder.Entity("DTPersonalInfoTracker.Models.LogModel", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id"), 1L, 1); + + b.Property("Date") + .HasColumnType("datetime2"); + + b.Property("Message") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("OperationName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Logs"); + }); + modelBuilder.Entity("DTPersonalInfoTracker.Models.PersonelModel", b => { b.Property("RecordId") diff --git a/Models/LogModel.cs b/Models/LogModel.cs new file mode 100644 index 0000000..5cfa0a8 --- /dev/null +++ b/Models/LogModel.cs @@ -0,0 +1,9 @@ +namespace DTPersonalInfoTracker.Models; + +public class LogModel +{ + public int Id { get; set; } + public string Message { get; set; } + public string OperationName { get; set; } + public DateTime Date { get; set; } +} \ No newline at end of file