using Microsoft.Data.SqlClient; using Quadarax.Application.QLiberace.Base; using Quadarax.Application.QLiberace.Base.Entities; using Quadarax.Application.QLiberace.Base.Repositories; using Quadarax.Application.QLiberace.Common.Domain; using Quadarax.Application.QLiberace.Console.Commands.Base; using Quadarax.Foundation.Core.Data; using Quadarax.Foundation.Core.Data.Domain; using Quadarax.Foundation.Core.Data.Interface.Domain; using Quadarax.Foundation.Core.QConsole; using Quadarax.Foundation.Core.QConsole.Attributes; using Quadarax.Foundation.Core.Value; namespace Quadarax.Application.QLiberace.Console.Commands { [CommandDefinition] internal class DbInitCmd : DbContextCommand { #region *** Properties *** public override string Name => Constants.Commands.DbInit.Name; public override string Description => Constants.Commands.DbInit.Description; #endregion #region *** Fields *** #endregion #region *** Constructor *** public DbInitCmd(Engine engine) : base(engine) { } #endregion #region *** EXECUTE *** protected override Result OnExecute(IDomainContextResolver resolver) { WriteInfo("Initializing database data..."); var resultCode = Constants.ReturnCodes.Ok; try { var uow = new UnitOfWork(resolver); var rtenant = new RepoTenant(uow.As(), new QlbrcContext(null, null, 1)); var tenant = rtenant.GetByCode("default"); if (tenant == null) { tenant = rtenant.New(); tenant.Code = "default"; tenant.Name = "Default tenant"; tenant.IsLocked = false; rtenant.Set(tenant); uow.Commit(); WriteInfo($"Tenant: '{tenant.Code}' created"); } else { WriteCaption($"Tenant: '{tenant.Code}' already exists"); } var tenantTst = rtenant.GetByCode("test"); if (tenantTst == null) { tenantTst = rtenant.New(); tenantTst.Code = "test"; tenantTst.Name = "Test tenant"; tenantTst.IsLocked = false; uow.Commit(); WriteInfo($"Tenant: '{tenant.Code}' created"); } else { WriteCaption($"Tenant: '{tenant.Code}' already exists"); } var ruser = new RepoUser(uow.As()); var user = ruser.GetByLoginName("testuser"); if (user == null) { user = ruser.New(); user.LoginName = "testuser"; tenant.IsLocked = false; uow.Commit(); WriteInfo($"User: '{user.LoginName}' created"); } else { WriteCaption($"User: '{user.LoginName}' already exists with '{user.Tenants.Count}' assignments"); } WriteInfo($"Assigning user '{user.LoginName}' to tenants.."); var tenant1Id = rtenant.GetByCode("default")?.Id; var tenant2Id = rtenant.GetByCode("test")?.Id; var assign = new TenantUser(); assign.UserId = user.Id; assign.TenantId = tenant1Id.GetValueOrDefault(); assign = new TenantUser(); assign.UserId = user.Id; assign.TenantId = tenant2Id.GetValueOrDefault(); uow.Commit(); WriteInfo("Assigned."); var rcountry = new RepoCountry(uow.As(), new QlbrcContext(null,null,null)); var rcurr = new RepoCurrency(uow.As(), new QlbrcContext(null,null,null)); var curUsd = EnsureCurrency(rcurr, "USD", "United States dollar", @"$"); var curEur = EnsureCurrency(rcurr, "EUR", "Euro", @"€"); var curCzk = EnsureCurrency(rcurr, "CZK", "Czech koruna", @"Kč"); EnsureCountry(rcountry, "CZE", "Czechia", 21, curCzk); EnsureCountry(rcountry, "USA", "United States of America", 0, curUsd); EnsureCountry(rcountry, "SVK", "Slovakia", 21, curEur); uow.Commit(); var currencies = rcurr.GetAllGlobal(Paging.NoPaging()); foreach (var curr in currencies) { if (curr.Tenants.All(x => x.TenantId != tenant.Id)) { curr.Tenants.Add(new TenantCurrency() { Currency = curr, Tenant = tenant, }); WriteInfo($"Assigned currency '{curr.Code}' to tenant '{tenant.Code}'"); } if (curr.Tenants.All(x => x.TenantId != tenantTst.Id)) { curr.Tenants.Add(new TenantCurrency() { Currency = curr, Tenant = tenantTst, }); WriteInfo($"Assigned currency '{curr.Code}' to tenant '{tenantTst.Code}'"); } } var countries = rcountry.GetAllGlobal(Paging.NoPaging()); foreach (var country in countries) { if (country.Tenants.All(x => x.TenantId != tenant.Id)) { country.Tenants.Add(new TenantCountry() { Country = country, Tenant = tenant, }); WriteInfo($"Assigned country '{country.Code}' to tenant '{tenant.Code}'"); } if (country.Tenants.All(x => x.TenantId != tenantTst.Id)) { country.Tenants.Add(new TenantCountry() { Country = country, Tenant = tenantTst, }); WriteInfo($"Assigned country '{country.Code}' to tenant '{tenantTst.Code}'"); } } uow.Commit(); } catch (SqlException e) { return HandleConnectionException(e); } return new Result(resultCode); } #endregion #region *** Private operations *** private Currency EnsureCurrency(RepoCurrency repo, string code, string name, string sign) { var cur = repo.GetByCode(code); if (cur == null) { cur = repo.New(); cur.Code = code; cur.Name = name; cur.Sign = sign; WriteInfo($"Currency: '{cur?.Code}' created"); } else { WriteCaption($"Currency: '{cur?.Code}' already exists"); } return cur!; } private Country EnsureCountry(RepoCountry repo, string code, string name, int vat, Currency defaultCurrency) { var country = repo.GetByCode(code); if (country == null) { country = repo.New(); country.Code = code; country.Name = name; country.Vat = vat; country.DefaultCurrency = defaultCurrency; WriteInfo($"Country: '{country?.Code}' created"); } else { WriteCaption($"Country: '{country?.Code}' already exists"); } return country!; } #endregion } }