| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- 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<QlbrcDbContext>(resolver);
- var rtenant = new RepoTenant(uow.As<IDataDomain>(), 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<IDataDomain>());
- 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<IDataDomain>(), new QlbrcContext(null,null,null));
- var rcurr = new RepoCurrency(uow.As<IDataDomain>(), 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
- }
- }
|