From a42b7d0ec42f78dfb5d87ee12ae9b0ef3226eee0 Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Tue, 7 Mar 2023 09:09:42 +0100 Subject: [PATCH] Inizio costruzione EFCore layer --- WebDoorCreator.Core/Class1.cs | 7 -- WebDoorCreator.Core/Enum.cs | 15 +++ .../WebDoorCreator.Core.csproj | 1 + .../Controllers/WebDoorCreatorController.cs | 17 ++- WebDoorCreator.Data/DbModels/CompanyModel.cs | 76 +++++++++++++ WebDoorCreator.Data/ModelBuilderExtensions.cs | 78 ++++++++++++++ WebDoorCreator.Data/RoleConfiguration.cs | 30 ++++++ WebDoorCreator.Data/WDCDataContext.cs | 102 ++++++++++++++++++ .../WebDoorCreator.Data.csproj | 2 +- WebDoorCreator.UI/appsettings.json | 2 +- 10 files changed, 319 insertions(+), 11 deletions(-) delete mode 100644 WebDoorCreator.Core/Class1.cs create mode 100644 WebDoorCreator.Core/Enum.cs create mode 100644 WebDoorCreator.Data/DbModels/CompanyModel.cs create mode 100644 WebDoorCreator.Data/ModelBuilderExtensions.cs create mode 100644 WebDoorCreator.Data/RoleConfiguration.cs create mode 100644 WebDoorCreator.Data/WDCDataContext.cs diff --git a/WebDoorCreator.Core/Class1.cs b/WebDoorCreator.Core/Class1.cs deleted file mode 100644 index 0729a6a..0000000 --- a/WebDoorCreator.Core/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace WebDoorCreator.Core -{ - public class Class1 - { - - } -} \ No newline at end of file diff --git a/WebDoorCreator.Core/Enum.cs b/WebDoorCreator.Core/Enum.cs new file mode 100644 index 0000000..cfb7387 --- /dev/null +++ b/WebDoorCreator.Core/Enum.cs @@ -0,0 +1,15 @@ +namespace WebDoorCreator.Core +{ + public class Enum + { + public enum UserLevel + { + ND = 0, + SuperAdmin = 1, + Admin = 2, + CompAdmin = 3, + CompUser = 4, + User = 5 + } + } +} \ No newline at end of file diff --git a/WebDoorCreator.Core/WebDoorCreator.Core.csproj b/WebDoorCreator.Core/WebDoorCreator.Core.csproj index db6c888..1f04627 100644 --- a/WebDoorCreator.Core/WebDoorCreator.Core.csproj +++ b/WebDoorCreator.Core/WebDoorCreator.Core.csproj @@ -7,6 +7,7 @@ + diff --git a/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs b/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs index 8865cd1..b95f70f 100644 --- a/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs +++ b/WebDoorCreator.Data/Controllers/WebDoorCreatorController.cs @@ -1,4 +1,6 @@ -using System; +using Microsoft.Extensions.Configuration; +using NLog; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,18 @@ using System.Threading.Tasks; namespace WebDoorCreator.Data.Controllers { - public class WebDoorCreatorController + public class WebDoorCreatorController : IDisposable { + private static IConfiguration _configuration; + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + public WebDoorCreatorController(IConfiguration configuration) + { + _configuration = configuration; + } + public void Dispose() + { + // Clear database context + //Log.Info("Dispose di GWMSController"); + } } } diff --git a/WebDoorCreator.Data/DbModels/CompanyModel.cs b/WebDoorCreator.Data/DbModels/CompanyModel.cs new file mode 100644 index 0000000..bb6d279 --- /dev/null +++ b/WebDoorCreator.Data/DbModels/CompanyModel.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +// +// This is here so CodeMaid doesn't reorganize this document +// +namespace WebDoorCreator.Data.DbModels +{ + /// + /// Tabella dati Company + /// + [Table("Company")] + public class CompanyModel + { + [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] + public int CompanyId { get; set; } + + /// + /// Codice esterno x riferimento (es ERP) + /// + [MaxLength(250)] + public string CompanyExtCode { get; set; } = ""; + + /// + /// Nome / ragione Sociale + /// + [MaxLength(500)] + public string CompanyName { get; set; } = ""; + + /// + /// indirizzo + /// + [MaxLength(250)] + public string Address { get; set; } = ""; + + /// + /// CAP + /// + public int ZipCode { get; set; } = 0; + + /// + /// Citta + /// + [MaxLength(50)] + public string City { get; set; } = ""; + + /// + /// Stato + /// + [MaxLength(50)] + public string State { get; set; } = ""; + + /// + /// VAT / P.Iva + /// + [MaxLength(50)] + public string VAT { get; set; } = ""; + + /// + /// Note private + /// + [MaxLength(500)] + public string PrivateNote { get; set; } = ""; + + /// + /// Codice GUID x collegare utente + /// + [MaxLength(150)] + public string CompanyToken { get; set; } = ""; + } +} diff --git a/WebDoorCreator.Data/ModelBuilderExtensions.cs b/WebDoorCreator.Data/ModelBuilderExtensions.cs new file mode 100644 index 0000000..f410b14 --- /dev/null +++ b/WebDoorCreator.Data/ModelBuilderExtensions.cs @@ -0,0 +1,78 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Emit; +using System.Text; +using System.Threading.Tasks; +//using WebDoorCreator.Data.DbModels; + +namespace WebDoorCreator.Data +{ + public static class ModelBuilderExtensions + { + /// + /// Estensione per seed iniziale dei dati nel DB + /// + /// + public static void Seed(this ModelBuilder modelBuilder) + { +#if false + // inizializzazione dei valori di default x USER + modelBuilder.Entity().HasData( + new UserModel { UserId = 1, AuthKey = "th1sIsTh3R1vrOfThNgt98", Livello = UserLevel.SuperAdmin, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 0, UserName = "samuele.locatelli", Email = "samuele@steamware.net", Firstname = "Samuele", Lastname = "Locatelli" }, + new UserModel { UserId = 2, AuthKey = "th1sIsTh3R1vrOfThNgt91", Livello = UserLevel.SuperAdmin, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 0, UserName = "giancarlo.rottoli", Email = "giancarlo@steamware.net", Firstname = "Giancarlo", Lastname = "Rottoli" }, + new UserModel { UserId = 3, AuthKey = "th1sIsTh3R1vrOfThNgt93", Livello = UserLevel.SuperAdmin, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 0, UserName = "steamw.admin", Email = "info@steamware.net", Firstname = "Steamware", Lastname = "Admin" }, + new UserModel { UserId = 4, AuthKey = "th1sIsTh3R1vrOfThNgt97", Livello = UserLevel.Admin, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 0, UserName = "angelo.pizzaferri", Email = "a.pizzaferri@pizzaferripetroli.it", Firstname = "Angelo", Lastname = "Pizzaferri" }, + new UserModel { UserId = 5, AuthKey = "th1sIsTh3R1vrOfThNgt99", Livello = UserLevel.Admin, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 0, UserName = "andrei.valeanu", Email = "andrei.valeanu@winnlab.it", Firstname = "Andrei", Lastname = "Valeanu" }, + new UserModel { UserId = 6, AuthKey = "th1sIsTh3R1vrOfThNgt92", Livello = UserLevel.UserExt, MaskPlantId = 0, MaskSupplierId = 1, MaskTranspId = 0, UserName = "liquigas.user01", Email = "info@steamware.net", Firstname = "User", Lastname = "LIQUIGAS" }, + new UserModel { UserId = 7, AuthKey = "th1sIsTh3R1vrOfThNgt94", Livello = UserLevel.UserExt, MaskPlantId = 0, MaskSupplierId = 2, MaskTranspId = 0, UserName = "vulkangas.user01", Email = "info@steamware.net", Firstname = "User", Lastname = "VULKANGAS" }, + new UserModel { UserId = 8, AuthKey = "th1sIsTh3R1vrOfThNgt95", Livello = UserLevel.UserExt, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 1, UserName = "levorato.user01", Email = "info@steamware.net", Firstname = "User", Lastname = "LEVORATO" }, + new UserModel { UserId = 9, AuthKey = "th1sIsTh3R1vrOfThNgt96", Livello = UserLevel.UserExt, MaskPlantId = 0, MaskSupplierId = 0, MaskTranspId = 2, UserName = "traffik.user01", Email = "info@steamware.net", Firstname = "User", Lastname = "TRAFFIK" }, + new UserModel { UserId = 10, AuthKey = "th1sIsTh3R1vrOfThNgt96", Livello = UserLevel.User, MaskPlantId = 1, MaskSupplierId = 0, MaskTranspId = 0, UserName = "piz03.user01", Email = "info@steamware.net", Firstname = "Stazione", Lastname = "Collecchio" }, + new UserModel { UserId = 11, AuthKey = "th1sIsTh3R1vrOfThNgt96", Livello = UserLevel.User, MaskPlantId = 2, MaskSupplierId = 0, MaskTranspId = 0, UserName = "piz04.user01", Email = "info@steamware.net", Firstname = "Stazione", Lastname = "Noceto" }, + new UserModel { UserId = 12, AuthKey = "th1sIsTh3R1vrOfThNgt96", Livello = UserLevel.User, MaskPlantId = 3, MaskSupplierId = 0, MaskTranspId = 0, UserName = "piz05.user01", Email = "info@steamware.net", Firstname = "Stazione", Lastname = "Baganzola" }, + new UserModel { UserId = 13, AuthKey = "th1sIsTh3R1vrOfThNgt96", Livello = UserLevel.User, MaskPlantId = 4, MaskSupplierId = 0, MaskTranspId = 0, UserName = "piz08.user01", Email = "info@steamware.net", Firstname = "Stazione", Lastname = "Pilastrello" } + ); +#endif + + //// inizializzazione dei valori di default x Plant + //modelBuilder.Entity().HasData( + // new PlantDetailModel { PlantId = 1, PlantCode = "PIZ03", PlantDesc = "Collecchio", LevelMax = 26000, LevelReorder = 15000, OrderQtyStd = 18000 }, + // new PlantDetailModel { PlantId = 2, PlantCode = "PIZ04", PlantDesc = "Noceto", LevelMax = 28000, LevelReorder = 15000, OrderQtyStd = 18000 }, + // new PlantDetailModel { PlantId = 3, PlantCode = "PIZ05", PlantDesc = "Baganzola", LevelMax = 24000, LevelReorder = 15000, OrderQtyStd = 18000 }, + // new PlantDetailModel { PlantId = 4, PlantCode = "PIZ08", PlantDesc = "Pilastrello", LevelMax = 26000, LevelReorder = 15000, OrderQtyStd = 18000 }, + // new PlantDetailModel { PlantId = 5, PlantCode = "PIZ09", PlantDesc = "Guardamiglio", LevelMax = 26000, LevelReorder = 15000, OrderQtyStd = 18000 } + // // new PlantDetailModel { PlantId = 1, PlantCode = "PIZ03", PlantDesc = "Collecchio", LevelMax = 26000, PressMax = 19, PressBHMax = 270, PressBLMax = 270 }, + // //new PlantDetailModel { PlantId = 2, PlantCode = "PIZ04", PlantDesc = "Noceto", LevelMax = 28000, PressMax = 19, PressBHMax = 270, PressBLMax = 270 }, + // //new PlantDetailModel { PlantId = 3, PlantCode = "PIZ05", PlantDesc = "Baganzola", LevelMax = 24000, PressMax = 19, PressBHMax = 270, PressBLMax = 270 }, + // //new PlantDetailModel { PlantId = 4, PlantCode = "PIZ08", PlantDesc = "Pilastrello", LevelMax = 26000, PressMax = 19, PressBHMax = 270, PressBLMax = 270 } + // ); + + //// inizializzazione dei valori di default x Fornitori + //modelBuilder.Entity().HasData( + // new SupplierModel { SupplierId = 1, SupplierCode = "LIQUIGAS", SupplierDesc = "Liquigas" }, + // new SupplierModel { SupplierId = 2, SupplierCode = "VULKANGAS", SupplierDesc = "Vulkangas" } + // ); + + //// inizializzazione dei valori di default x Trasportatori + //modelBuilder.Entity().HasData( + // new TransporterModel { TransporterId = 1, TransporterCode = "LEVO", TransporterDesc = "Levorato" }, + // new TransporterModel { TransporterId = 2, TransporterCode = "TRAF", TransporterDesc = "Traffik" } + // ); + + //// init consegne... + //modelBuilder.Entity().HasData( + // new WeekPlanModel { WeekPlanId = 1, DayNum = DayOfWeek.Monday, DeliveryHour = 20, Note = "18K", PlantId = 2, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 2, DayNum = DayOfWeek.Tuesday, DeliveryHour = 20, Note = "18K", PlantId = 2, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 3, DayNum = DayOfWeek.Wednesday, DeliveryHour = 20, Note = "18K", PlantId = 2, SupplierId = 1, TransporterId = 2 }, + // new WeekPlanModel { WeekPlanId = 4, DayNum = DayOfWeek.Thursday, DeliveryHour = 15, Note = "9K", PlantId = 2, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 5, DayNum = DayOfWeek.Thursday, DeliveryHour = 20, Note = "18K", PlantId = 2, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 6, DayNum = DayOfWeek.Saturday, DeliveryHour = 20, Note = "18K", PlantId = 2, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 7, DayNum = DayOfWeek.Tuesday, DeliveryHour = 14, Note = "3K", PlantId = 3, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 8, DayNum = DayOfWeek.Tuesday, DeliveryHour = 15, Note = "15K", PlantId = 4, SupplierId = 1, TransporterId = 1 }, + // new WeekPlanModel { WeekPlanId = 9, DayNum = DayOfWeek.Tuesday, DeliveryHour = 17, Note = "18K", PlantId = 1, SupplierId = 2, TransporterId = 2 } + // ); + } + } +} diff --git a/WebDoorCreator.Data/RoleConfiguration.cs b/WebDoorCreator.Data/RoleConfiguration.cs new file mode 100644 index 0000000..2b90212 --- /dev/null +++ b/WebDoorCreator.Data/RoleConfiguration.cs @@ -0,0 +1,30 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WebDoorCreator.Data +{ + public class RoleConfiguration : IEntityTypeConfiguration + { + #region Public Methods + + public void Configure(EntityTypeBuilder builder) + { + builder.HasData( + new IdentityRole { Name = "Undef", NormalizedName = "UNDEF" }, + new IdentityRole { Name = "SuperAdmin", NormalizedName = "SUPERADMIN" }, + new IdentityRole { Name = "Admin", NormalizedName = "ADMIN" }, + new IdentityRole { Name = "CompAdmin", NormalizedName = "COMPADMIN" }, + new IdentityRole { Name = "CompUser", NormalizedName = "COMPUSER" }, + new IdentityRole { Name = "User", NormalizedName = "USER" } + ); + } + + #endregion Public Methods + } +} diff --git a/WebDoorCreator.Data/WDCDataContext.cs b/WebDoorCreator.Data/WDCDataContext.cs new file mode 100644 index 0000000..6079273 --- /dev/null +++ b/WebDoorCreator.Data/WDCDataContext.cs @@ -0,0 +1,102 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using NLog; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WebDoorCreator.Data.DbModels; +using static Org.BouncyCastle.Math.EC.ECCurve; + +namespace WebDoorCreator.Data +{ + public partial class WDCDataContext : DbContext + { + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private IConfiguration _configuration; + public WDCDataContext() + { + } + + public WDCDataContext(IConfiguration configuration) + { + _configuration = configuration; + } + + public WDCDataContext(DbContextOptions options) : base(options) + { + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + } + catch (Exception exc) + { + Log.Error(exc, "Exception during context initialization 02"); + } + } + + + public virtual DbSet DbSetCompany { get; set; } + + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) + { + string connString = _configuration.GetConnectionString("WDC.DB"); + if (!string.IsNullOrEmpty(connString)) + { + optionsBuilder.UseSqlServer(connString); + } + else + { + optionsBuilder.UseSqlServer("Server=SQL2016DEV;Database=WebDoorCreator;Trusted_Connection=True;"); + } + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.UseCollation("Latin1_General_CI_AS"); + //foreach (var relationship in modelBuilder.Model.GetEntityTypes().SelectMany(e => e.GetForeignKeys())) + //{ + // relationship.DeleteBehavior = DeleteBehavior.Restrict; + //} + + //modelBuilder.Entity(entity => + //{ + // entity.Property(e => e.ValStd) + // .HasComment("Valore di default/riferimento per la variabile"); + //}); + + //modelBuilder.Entity().HasKey(c => new { c.TabName, c.FieldName, c.Val }); + + //modelBuilder.Entity().HasKey(c => new { c.PlantId, c.FluxType }); + + //modelBuilder.Entity().HasKey(c => new { c.PlantId, c.ParamUid }); + + modelBuilder.Seed(); + + OnModelCreatingPartial(modelBuilder); + } + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + public void DbForceMigrate() + { + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + Log.Info("DbForceMigrate: done!"); + } + catch (Exception exc) + { + Log.Error(exc, "DbForceMigrate: Exception during context initialization 01"); + } + } + } +} diff --git a/WebDoorCreator.Data/WebDoorCreator.Data.csproj b/WebDoorCreator.Data/WebDoorCreator.Data.csproj index be8fbba..66a6b55 100644 --- a/WebDoorCreator.Data/WebDoorCreator.Data.csproj +++ b/WebDoorCreator.Data/WebDoorCreator.Data.csproj @@ -20,7 +20,7 @@ - + diff --git a/WebDoorCreator.UI/appsettings.json b/WebDoorCreator.UI/appsettings.json index 861c31d..3b2a185 100644 --- a/WebDoorCreator.UI/appsettings.json +++ b/WebDoorCreator.UI/appsettings.json @@ -9,7 +9,7 @@ "ConnectionStrings": { "Identity.DB": "Server=SQL2016DEV;Database=WebDoorCreator; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=WebDoorCreator.UI;", "Redis": "localhost:6379,DefaultDatabase=15,connectTimeout=5000,syncTimeout=5000,asyncTimeout=5000,abortConnect=false,ssl=false", - "ApplicationDbContextConnection": "Server=(localdb)\\mssqllocaldb;Database=WebDoorCreator.UI;Trusted_Connection=True;MultipleActiveResultSets=true" + "WDC.DB": "Server=SQL2016DEV;Database=WebDoorCreator; User ID=sa;Password=keyhammer16; integrated security=False; MultipleActiveResultSets=True; App=WebDoorCreator.UI;" }, "ExternalProviders": { "MailKit": {