DbInitCmd.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. using Microsoft.Data.SqlClient;
  2. using Quadarax.Application.QLiberace.Base;
  3. using Quadarax.Application.QLiberace.Base.Entities;
  4. using Quadarax.Application.QLiberace.Base.Repositories;
  5. using Quadarax.Application.QLiberace.Common.Domain;
  6. using Quadarax.Application.QLiberace.Console.Commands.Base;
  7. using Quadarax.Foundation.Core.Data;
  8. using Quadarax.Foundation.Core.Data.Domain;
  9. using Quadarax.Foundation.Core.Data.Interface.Domain;
  10. using Quadarax.Foundation.Core.QConsole;
  11. using Quadarax.Foundation.Core.QConsole.Attributes;
  12. using Quadarax.Foundation.Core.Value;
  13. namespace Quadarax.Application.QLiberace.Console.Commands
  14. {
  15. [CommandDefinition]
  16. internal class DbInitCmd : DbContextCommand
  17. {
  18. #region *** Properties ***
  19. public override string Name => Constants.Commands.DbInit.Name;
  20. public override string Description => Constants.Commands.DbInit.Description;
  21. #endregion
  22. #region *** Fields ***
  23. #endregion
  24. #region *** Constructor ***
  25. public DbInitCmd(Engine engine) : base(engine)
  26. {
  27. }
  28. #endregion
  29. #region *** EXECUTE ***
  30. protected override Result OnExecute(IDomainContextResolver resolver)
  31. {
  32. WriteInfo("Initializing database data...");
  33. var resultCode = Constants.ReturnCodes.Ok;
  34. try
  35. {
  36. var uow = new UnitOfWork<QlbrcDbContext>(resolver);
  37. var rtenant = new RepoTenant(uow.As<IDataDomain>(), new QlbrcContext(null, null, 1));
  38. var tenant = rtenant.GetByCode("default");
  39. if (tenant == null)
  40. {
  41. tenant = rtenant.New();
  42. tenant.Code = "default";
  43. tenant.Name = "Default tenant";
  44. tenant.IsLocked = false;
  45. rtenant.Set(tenant);
  46. uow.Commit();
  47. WriteInfo($"Tenant: '{tenant.Code}' created");
  48. }
  49. else
  50. {
  51. WriteCaption($"Tenant: '{tenant.Code}' already exists");
  52. }
  53. var tenantTst = rtenant.GetByCode("test");
  54. if (tenantTst == null)
  55. {
  56. tenantTst = rtenant.New();
  57. tenantTst.Code = "test";
  58. tenantTst.Name = "Test tenant";
  59. tenantTst.IsLocked = false;
  60. uow.Commit();
  61. WriteInfo($"Tenant: '{tenant.Code}' created");
  62. }
  63. else
  64. {
  65. WriteCaption($"Tenant: '{tenant.Code}' already exists");
  66. }
  67. var ruser = new RepoUser(uow.As<IDataDomain>());
  68. var user = ruser.GetByLoginName("testuser");
  69. if (user == null)
  70. {
  71. user = ruser.New();
  72. user.LoginName = "testuser";
  73. tenant.IsLocked = false;
  74. uow.Commit();
  75. WriteInfo($"User: '{user.LoginName}' created");
  76. }
  77. else
  78. {
  79. WriteCaption($"User: '{user.LoginName}' already exists with '{user.Tenants.Count}' assignments");
  80. }
  81. WriteInfo($"Assigning user '{user.LoginName}' to tenants..");
  82. var tenant1Id = rtenant.GetByCode("default")?.Id;
  83. var tenant2Id = rtenant.GetByCode("test")?.Id;
  84. var assign = new TenantUser();
  85. assign.UserId = user.Id;
  86. assign.TenantId = tenant1Id.GetValueOrDefault();
  87. assign = new TenantUser();
  88. assign.UserId = user.Id;
  89. assign.TenantId = tenant2Id.GetValueOrDefault();
  90. uow.Commit();
  91. WriteInfo("Assigned.");
  92. var rcountry = new RepoCountry(uow.As<IDataDomain>(), new QlbrcContext(null,null,null));
  93. var rcurr = new RepoCurrency(uow.As<IDataDomain>(), new QlbrcContext(null,null,null));
  94. var curUsd = EnsureCurrency(rcurr, "USD", "United States dollar", @"$");
  95. var curEur = EnsureCurrency(rcurr, "EUR", "Euro", @"€");
  96. var curCzk = EnsureCurrency(rcurr, "CZK", "Czech koruna", @"Kč");
  97. EnsureCountry(rcountry, "CZE", "Czechia", 21, curCzk);
  98. EnsureCountry(rcountry, "USA", "United States of America", 0, curUsd);
  99. EnsureCountry(rcountry, "SVK", "Slovakia", 21, curEur);
  100. uow.Commit();
  101. var currencies = rcurr.GetAllGlobal(Paging.NoPaging());
  102. foreach (var curr in currencies)
  103. {
  104. if (curr.Tenants.All(x => x.TenantId != tenant.Id))
  105. {
  106. curr.Tenants.Add(new TenantCurrency()
  107. {
  108. Currency = curr,
  109. Tenant = tenant,
  110. });
  111. WriteInfo($"Assigned currency '{curr.Code}' to tenant '{tenant.Code}'");
  112. }
  113. if (curr.Tenants.All(x => x.TenantId != tenantTst.Id))
  114. {
  115. curr.Tenants.Add(new TenantCurrency()
  116. {
  117. Currency = curr,
  118. Tenant = tenantTst,
  119. });
  120. WriteInfo($"Assigned currency '{curr.Code}' to tenant '{tenantTst.Code}'");
  121. }
  122. }
  123. var countries = rcountry.GetAllGlobal(Paging.NoPaging());
  124. foreach (var country in countries)
  125. {
  126. if (country.Tenants.All(x => x.TenantId != tenant.Id))
  127. {
  128. country.Tenants.Add(new TenantCountry()
  129. {
  130. Country = country,
  131. Tenant = tenant,
  132. });
  133. WriteInfo($"Assigned country '{country.Code}' to tenant '{tenant.Code}'");
  134. }
  135. if (country.Tenants.All(x => x.TenantId != tenantTst.Id))
  136. {
  137. country.Tenants.Add(new TenantCountry()
  138. {
  139. Country = country,
  140. Tenant = tenantTst,
  141. });
  142. WriteInfo($"Assigned country '{country.Code}' to tenant '{tenantTst.Code}'");
  143. }
  144. }
  145. uow.Commit();
  146. }
  147. catch (SqlException e)
  148. {
  149. return HandleConnectionException(e);
  150. }
  151. return new Result(resultCode);
  152. }
  153. #endregion
  154. #region *** Private operations ***
  155. private Currency EnsureCurrency(RepoCurrency repo, string code, string name, string sign)
  156. {
  157. var cur = repo.GetByCode(code);
  158. if (cur == null)
  159. {
  160. cur = repo.New();
  161. cur.Code = code;
  162. cur.Name = name;
  163. cur.Sign = sign;
  164. WriteInfo($"Currency: '{cur?.Code}' created");
  165. }
  166. else
  167. {
  168. WriteCaption($"Currency: '{cur?.Code}' already exists");
  169. }
  170. return cur!;
  171. }
  172. private Country EnsureCountry(RepoCountry repo, string code, string name, int vat, Currency defaultCurrency)
  173. {
  174. var country = repo.GetByCode(code);
  175. if (country == null)
  176. {
  177. country = repo.New();
  178. country.Code = code;
  179. country.Name = name;
  180. country.Vat = vat;
  181. country.DefaultCurrency = defaultCurrency;
  182. WriteInfo($"Country: '{country?.Code}' created");
  183. }
  184. else
  185. {
  186. WriteCaption($"Country: '{country?.Code}' already exists");
  187. }
  188. return country!;
  189. }
  190. #endregion
  191. }
  192. }