From fd696ae3f9de7ce4bafa4ea7ba3c2684f8348f4f Mon Sep 17 00:00:00 2001 From: Samuele Locatelli Date: Wed, 9 Feb 2022 18:58:20 +0100 Subject: [PATCH] Aggiunta proj MP.MONO.Data versione preliminare --- MP.MONO.Data/AdminContext.cs | 79 +++++++++++ MP.MONO.Data/Class1.cs | 7 - MP.MONO.Data/DbAdmin.cs | 102 ++++++++++++++ MP.MONO.Data/DbConfig.cs | 78 +++++++++++ MP.MONO.Data/DbModels/TableCount.cs | 26 ++++ MP.MONO.Data/DbModels/UserPrivModel.cs | 29 ++++ MP.MONO.Data/MP.MONO.Data.csproj | 29 +++- MP.MONO.Data/MailKitEmailSender.cs | 65 +++++++++ MP.MONO.Data/MailKitEmailSenderOptions.cs | 31 +++++ MP.MONO.Data/MapoMonoContext.cs | 154 ++++++++++++++++++++++ MP.MONO.Data/ModelBuilderExtensions.cs | 81 ++++++++++++ MP.MONO.Data/RoleConfiguration.cs | 25 ++++ MP.MONO.Data/UserIdentityDbContext.cs | 67 ++++++++++ 13 files changed, 765 insertions(+), 8 deletions(-) create mode 100644 MP.MONO.Data/AdminContext.cs delete mode 100644 MP.MONO.Data/Class1.cs create mode 100644 MP.MONO.Data/DbAdmin.cs create mode 100644 MP.MONO.Data/DbConfig.cs create mode 100644 MP.MONO.Data/DbModels/TableCount.cs create mode 100644 MP.MONO.Data/DbModels/UserPrivModel.cs create mode 100644 MP.MONO.Data/MailKitEmailSender.cs create mode 100644 MP.MONO.Data/MailKitEmailSenderOptions.cs create mode 100644 MP.MONO.Data/MapoMonoContext.cs create mode 100644 MP.MONO.Data/ModelBuilderExtensions.cs create mode 100644 MP.MONO.Data/RoleConfiguration.cs create mode 100644 MP.MONO.Data/UserIdentityDbContext.cs diff --git a/MP.MONO.Data/AdminContext.cs b/MP.MONO.Data/AdminContext.cs new file mode 100644 index 0000000..b0ae830 --- /dev/null +++ b/MP.MONO.Data/AdminContext.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using MP.MONO.Data.DbModels; + +namespace MP.MONO.Data +{ + public partial class AdminContext : DbContext + { + #region Private Fields + + private IConfiguration _configuration; + + #endregion Private Fields + + #region Public Constructors + + public AdminContext() + { + } + + public AdminContext(IConfiguration configuration) + { + _configuration = configuration; + } + + public AdminContext(DbContextOptions options) : base(options) + { + } + + #endregion Public Constructors + + #region Public Properties + + /// + /// User management + /// + public DbSet UserList { get; set; } + + #endregion Public Properties + + #region Private Methods + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + #endregion Private Methods + + #region Protected Methods + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + string connString = DbConfig.ADMIN_CONNECTION_STRING; + if (!optionsBuilder.IsConfigured) + { + //connString = _configuration.GetConnectionString("GWMS.Data"); + //connString = "Server=localhost;port=3306;database=GWMS;user=GWMS;pwd=GWMS_secret_pwd;sslmode=None;"; + var serverVersion = ServerVersion.AutoDetect(connString); + optionsBuilder.UseMySql(connString, serverVersion); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(c => new { c.Host, c.User }); + + OnModelCreatingPartial(modelBuilder); + } + + #endregion Protected Methods + } +} diff --git a/MP.MONO.Data/Class1.cs b/MP.MONO.Data/Class1.cs deleted file mode 100644 index 31f1110..0000000 --- a/MP.MONO.Data/Class1.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace MP.MONO.Data -{ - public class Class1 - { - - } -} \ No newline at end of file diff --git a/MP.MONO.Data/DbAdmin.cs b/MP.MONO.Data/DbAdmin.cs new file mode 100644 index 0000000..dcbfee3 --- /dev/null +++ b/MP.MONO.Data/DbAdmin.cs @@ -0,0 +1,102 @@ +using Microsoft.EntityFrameworkCore; +using NLog; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace MP.MONO.Data +{ + public class DbAdmin : IDisposable + { + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + #endregion Private Fields + + #region Public Constructors + + public DbAdmin() + { + } + + #endregion Public Constructors + + #region Public Methods + + public static bool checkCreateUser(string username, string pwd) + { + bool answ = false; + using (AdminContext adbCtx = new AdminContext()) + { + // ricerca utente... + var numUser = adbCtx + .UserList + .Where(x => x.User == username) + .ToList() + .Count; + if (numUser > 0) + { + answ = true; + } + if (!answ) + { + // creo utente + string sqlCommand = "FLUSH PRIVILEGES;"; + adbCtx.Database.ExecuteSqlRaw(sqlCommand); + sqlCommand = $"CREATE USER '{username}'@'localhost' IDENTIFIED BY '{pwd}';"; + adbCtx.Database.ExecuteSqlRaw(sqlCommand); + sqlCommand = $"GRANT ALL ON *.* TO '{username}'@'localhost';"; + adbCtx.Database.ExecuteSqlRaw(sqlCommand); + sqlCommand = "FLUSH PRIVILEGES;"; + adbCtx.Database.ExecuteSqlRaw(sqlCommand); + } + } + return answ; + } + + public static async Task migrateDbIdentity() + { + bool answ = false; + using (UserIdentityDbContext dbCtx = new UserIdentityDbContext()) + { + await dbCtx.Database.MigrateAsync(); + answ = true; + } + return answ; + } + + public static async Task migrateDbMain() + { + bool answ = false; +#if false + using (GWMSContext dbCtx = new GWMSContext()) + { + await dbCtx.Database.MigrateAsync(); + answ = true; + } +#endif + return answ; + } + +#if false + public static bool resetPlantLogTable() + { + bool answ = false; + using (GWMSContext dbCtx = new GWMSContext()) + { + string sqlCommand = "TRUNCATE TABLE PlantLog;"; + dbCtx.Database.ExecuteSqlRaw(sqlCommand); + answ = true; + } + return answ; + } +#endif + + public void Dispose() + { + } + + #endregion Public Methods + } +} diff --git a/MP.MONO.Data/DbConfig.cs b/MP.MONO.Data/DbConfig.cs new file mode 100644 index 0000000..012b818 --- /dev/null +++ b/MP.MONO.Data/DbConfig.cs @@ -0,0 +1,78 @@ +using Microsoft.EntityFrameworkCore; + +namespace MP.MONO.Data +{ + public static class DbConfig + { + #region Public Fields + + public static string DATABASE_NAME = "MAPO.MONO"; + + public static int DATABASE_PROCESS_TIMEOUT = 5; + public static string DATABASE_PWD = "viadante16"; + + // Database config + public static string DATABASE_SERV = "127.0.0.1"; + + public static string DATABASE_USER = "MAPO_MONO_User"; + + #endregion Public Fields + + #region Public Properties + + /// + /// DB Connection string per azioni amministrative + /// + public static string ADMIN_CONNECTION_STRING { get; set; } = ""; + + /// + /// DB Connection string + /// + public static string CONNECTION_STRING { get; set; } = ""; + + #endregion Public Properties + + #region Public Methods + + public static bool CheckUser(string nKey, string sKey) + { + // esecuzione script di install locale + return DbAdmin.checkCreateUser(DATABASE_USER, DATABASE_PWD); + } + + public static bool ExecMigrationIdentity() + { + // esecuzione migrazione + var migrateTask = Task.Run(async () => await DbAdmin.migrateDbIdentity()); + migrateTask.Wait(); + return migrateTask.Result; + } + + public static bool ExecMigrationMain() + { + // esecuzione migrazione + var migrateTask = Task.Run(async () => await DbAdmin.migrateDbMain()); + migrateTask.Wait(); + return migrateTask.Result; + } + + public static void InitDb(string server, string nKey, string sKey) + { + DATABASE_SERV = server; + DATABASE_NAME = $"GWMS_{nKey}"; + DATABASE_USER = $"user_{nKey}"; + DATABASE_PWD = $"pwd_{sKey}"; + CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database={DATABASE_NAME};uid={DATABASE_USER};pwd={DATABASE_PWD};sslmode=None"; + // stringa admin con utente root egalware... + ADMIN_CONNECTION_STRING = $"server={DATABASE_SERV};port=3306;database=mysql;uid=root;pwd=Egalware_24068!;sslmode=None"; + } + + public static ServerVersion MysqlServerVersion(string connString) + { + ServerVersion serverVersion = ServerVersion.AutoDetect(connString); + return serverVersion; + } + + #endregion Public Methods + } +} \ No newline at end of file diff --git a/MP.MONO.Data/DbModels/TableCount.cs b/MP.MONO.Data/DbModels/TableCount.cs new file mode 100644 index 0000000..15d8c83 --- /dev/null +++ b/MP.MONO.Data/DbModels/TableCount.cs @@ -0,0 +1,26 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +// +// This is here so CodeMaid doesn't reorganize this document +// +namespace MP.MONO.Data.DbModels +{ + /// + /// Classe fake x il conteggio tabelle e check preliminari + /// + [Keyless] + public class TableCount + { + #region Public Properties + + public int Count { get; set; } + public string TableName { get; set; } + + #endregion Public Properties + } +} diff --git a/MP.MONO.Data/DbModels/UserPrivModel.cs b/MP.MONO.Data/DbModels/UserPrivModel.cs new file mode 100644 index 0000000..2eef2ba --- /dev/null +++ b/MP.MONO.Data/DbModels/UserPrivModel.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +// +// This is here so CodeMaid doesn't reorganize this document +// +namespace MP.MONO.Data.DbModels +{ + /// + /// Tabella dei USER di MySql + /// + [Table("user")] + public class UserPriv + { + #region Public Properties + + [Column("Host", Order = 1)] + public string Host { get; set; } = ""; + + [Column("User", Order = 2)] + public string User { get; set; } = ""; + + #endregion Public Properties + } +} diff --git a/MP.MONO.Data/MP.MONO.Data.csproj b/MP.MONO.Data/MP.MONO.Data.csproj index 132c02c..af4f74c 100644 --- a/MP.MONO.Data/MP.MONO.Data.csproj +++ b/MP.MONO.Data/MP.MONO.Data.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,4 +6,31 @@ enable + + + + + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/MP.MONO.Data/MailKitEmailSender.cs b/MP.MONO.Data/MailKitEmailSender.cs new file mode 100644 index 0000000..c171d4a --- /dev/null +++ b/MP.MONO.Data/MailKitEmailSender.cs @@ -0,0 +1,65 @@ +using MailKit.Net.Smtp; +using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.Extensions.Options; +using MimeKit; +using MimeKit.Text; +using System.Threading.Tasks; + +namespace MP.MONO.Data +{ + /// + /// Implementazione interfaccia email con pacchetto MailKIT + /// + /// https://www.ryadel.com/en/asp-net-core-send-email-messages-smtp-mailkit/ + /// + public class MailKitEmailSender : IEmailSender + { + #region Public Constructors + + public MailKitEmailSender(IOptions options) + { + this.Options = options.Value; + } + + #endregion Public Constructors + + #region Public Properties + + public MailKitEmailSenderOptions Options { get; set; } + + #endregion Public Properties + + #region Public Methods + + public Task Execute(string to, string subject, string message) + { + // create message + var email = new MimeMessage(); + email.Sender = MailboxAddress.Parse(Options.Sender_EMail); + if (!string.IsNullOrEmpty(Options.Sender_Name)) + email.Sender.Name = Options.Sender_Name; + email.From.Add(email.Sender); + email.To.Add(MailboxAddress.Parse(to)); + email.Subject = subject; + email.Body = new TextPart(TextFormat.Html) { Text = message }; + + // send email + using (var smtp = new SmtpClient()) + { + smtp.Connect(Options.Host_Address, Options.Host_Port, Options.Host_SecureSocketOptions); + smtp.Authenticate(Options.Host_Username, Options.Host_Password); + smtp.Send(email); + smtp.Disconnect(true); + } + + return Task.FromResult(true); + } + + public Task SendEmailAsync(string email, string subject, string message) + { + return Execute(email, subject, message); + } + + #endregion Public Methods + } +} diff --git a/MP.MONO.Data/MailKitEmailSenderOptions.cs b/MP.MONO.Data/MailKitEmailSenderOptions.cs new file mode 100644 index 0000000..40e806e --- /dev/null +++ b/MP.MONO.Data/MailKitEmailSenderOptions.cs @@ -0,0 +1,31 @@ +using MailKit.Security; + +namespace MP.MONO.Data +{ + public class MailKitEmailSenderOptions + { + #region Public Constructors + + public MailKitEmailSenderOptions() + { + Host_SecureSocketOptions = SecureSocketOptions.Auto; + } + + #endregion Public Constructors + + #region Public Properties + + public string Host_Address { get; set; } + + public string Host_Password { get; set; } + public int Host_Port { get; set; } + + public SecureSocketOptions Host_SecureSocketOptions { get; set; } + public string Host_Username { get; set; } + public string Sender_EMail { get; set; } + + public string Sender_Name { get; set; } + + #endregion Public Properties + } +} diff --git a/MP.MONO.Data/MapoMonoContext.cs b/MP.MONO.Data/MapoMonoContext.cs new file mode 100644 index 0000000..b0d72d8 --- /dev/null +++ b/MP.MONO.Data/MapoMonoContext.cs @@ -0,0 +1,154 @@ +using MP.MONO.Data.DbModels; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using NLog; +using System; +using System.Linq; + +namespace MP.MONO.Data +{ + public partial class MapoMonoContext : DbContext + { + #region Private Fields + + private static NLog.Logger Log = LogManager.GetCurrentClassLogger(); + + private IConfiguration _configuration; + + #endregion Private Fields + + #region Public Constructors + + public MapoMonoContext() + { + } + + public MapoMonoContext(IConfiguration configuration) + { + _configuration = configuration; + } + + public MapoMonoContext(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"); + } + } + + #endregion Public Constructors + + #region Public Properties + +#if false + public virtual DbSet DbRebootLog { get; set; } + + public virtual DbSet DbSetAlarmLog { get; set; } + + public virtual DbSet DbSetConfig { get; set; } + + public virtual DbSet DbSetItems { get; set; } + + public virtual DbSet DbSetKeyVal { get; set; } + + public virtual DbSet DbSetListVal { get; set; } + + public virtual DbSet DbSetOrders { get; set; } + + public virtual DbSet DbSetParamSend { get; set; } + + public virtual DbSet DbSetParamSet { get; set; } + + public virtual DbSet DbSetPlant { get; set; } + + public virtual DbSet DbSetPlantLog { get; set; } + + public virtual DbSet DbSetPlantStatus { get; set; } + + public virtual DbSet DbSetPlantSupplWeekPlan { get; set; } + + public virtual DbSet DbSetSupplier { get; set; } + + public virtual DbSet DbSetTransporter { get; set; } +#endif + + #endregion Public Properties + + #region Private Methods + + partial void OnModelCreatingPartial(ModelBuilder modelBuilder); + + #endregion Private Methods + + #region Protected Methods + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + // default + string connString = "Server=localhost;port=3306;database=MAPO.MONO;user=MapoUsr;pwd=M@poUsrS3cretPass!;sslmode=None;"; + + // tento setup da config + try + { + // uso conn string calcolata + connString = DbConfig.CONNECTION_STRING; + } + catch + { } + if (!optionsBuilder.IsConfigured) + { + var serverVersion = ServerVersion.AutoDetect(connString); + optionsBuilder.UseMySql(connString, serverVersion); + } + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + 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); + } + + #endregion Protected Methods + + #region Public Methods + + 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"); + } + } + + #endregion Public Methods + } +} diff --git a/MP.MONO.Data/ModelBuilderExtensions.cs b/MP.MONO.Data/ModelBuilderExtensions.cs new file mode 100644 index 0000000..5692999 --- /dev/null +++ b/MP.MONO.Data/ModelBuilderExtensions.cs @@ -0,0 +1,81 @@ +using MP.MONO.Data.DbModels; +using Microsoft.EntityFrameworkCore; +using System; + +namespace MP.MONO.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 + +#if false + // 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 } + ); +#endif + +#if false + // 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" } + ); +#endif + +#if false + // 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" } + ); +#endif + +#if false + // 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 } + ); +#endif + } + } +} diff --git a/MP.MONO.Data/RoleConfiguration.cs b/MP.MONO.Data/RoleConfiguration.cs new file mode 100644 index 0000000..bef9410 --- /dev/null +++ b/MP.MONO.Data/RoleConfiguration.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace MP.MONO.Data +{ + public class RoleConfiguration : IEntityTypeConfiguration + { + #region Public Methods + + public void Configure(EntityTypeBuilder builder) + { + builder.HasData( + //new IdentityRole { Name = "Undef", NormalizedName = "UNDEF" }, + //new IdentityRole { Name = "ExtUser", NormalizedName = "EXTUSER" }, + //new IdentityRole { Name = "ExtTransp", NormalizedName = "EXTTRANSP" }, + new IdentityRole { Name = "User", NormalizedName = "USER" }, + new IdentityRole { Name = "Admin", NormalizedName = "ADMIN" }, + new IdentityRole { Name = "SuperAdmin", NormalizedName = "SUPERADMIN" } + ); + } + + #endregion Public Methods + } +} diff --git a/MP.MONO.Data/UserIdentityDbContext.cs b/MP.MONO.Data/UserIdentityDbContext.cs new file mode 100644 index 0000000..47ae428 --- /dev/null +++ b/MP.MONO.Data/UserIdentityDbContext.cs @@ -0,0 +1,67 @@ +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; +using MP.MONO.Data.DbModels; + +namespace MP.MONO.Data +{ + public class UserIdentityDbContext : IdentityDbContext + { + #region Public Constructors + + public UserIdentityDbContext() + { + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + } + catch (Exception exc) + { } + } + + public UserIdentityDbContext(DbContextOptions options) + : base(options) + { +#if false + // se non ci fosse... crea! + Database.EnsureCreated(); +#endif + try + { + // se non ci fosse... crea o migra! + Database.Migrate(); + } + catch (Exception exc) + { } + } + + #endregion Public Constructors + + #region Public Properties + + public DbSet DbSetCounts { get; set; } + + #endregion Public Properties + + #region Protected Methods + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + string connString = DbConfig.CONNECTION_STRING; + if (!optionsBuilder.IsConfigured) + { + var serverVersion = ServerVersion.AutoDetect(connString); + optionsBuilder.UseMySql(connString, serverVersion); + } + } + + protected override void OnModelCreating(ModelBuilder builder) + { + base.OnModelCreating(builder); + + builder.ApplyConfiguration(new RoleConfiguration()); + } + + #endregion Protected Methods + } +} \ No newline at end of file