DbInitCmd.cs 8.4 KB

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