DbInitCmd.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.Domain;
  13. using Quadarax.Foundation.Core.QConsole;
  14. using Quadarax.Foundation.Core.QConsole.Attributes;
  15. using Quadarax.Foundation.Core.Value;
  16. namespace Quadarax.Application.QLiberace.Console.Commands
  17. {
  18. [CommandDefinition]
  19. internal class DbInitCmd : DbContextCommand
  20. {
  21. #region *** Properties ***
  22. public override string Name => Constants.Commands.DbInit.Name;
  23. public override string Description => Constants.Commands.DbInit.Description;
  24. #endregion
  25. #region *** Fields ***
  26. #endregion
  27. #region *** Constructor ***
  28. public DbInitCmd(Engine engine) : base(engine)
  29. {
  30. }
  31. #endregion
  32. #region *** EXECUTE ***
  33. protected override Result OnExecute(QlbrcDbContext context)
  34. {
  35. WriteInfo("Initializing database data...");
  36. var resultCode = Constants.ReturnCodes.Ok;
  37. try
  38. {
  39. var dbcResolver = new DomainContextResolver<QlbrcDbContext>(context);
  40. var uow = new UnitOfWork<QlbrcDbContext>(dbcResolver);
  41. var rtenant = new RepoTenant(uow.As<IDataDomain>(), new QlbrcContext(null, null, 1));
  42. var tenant = rtenant.GetByCode("default");
  43. if (tenant == null)
  44. {
  45. tenant = rtenant.New();
  46. tenant.Code = "default";
  47. tenant.Name = "Default tenant";
  48. tenant.IsLocked = false;
  49. rtenant.Set(tenant);
  50. uow.Commit();
  51. WriteInfo($"Tenant: '{tenant.Code}' created");
  52. }
  53. else
  54. {
  55. WriteCaption($"Tenant: '{tenant.Code}' already exists");
  56. }
  57. tenant = rtenant.GetByCode("test");
  58. if (tenant == null)
  59. {
  60. tenant = rtenant.New();
  61. tenant.Code = "test";
  62. tenant.Name = "Test tenant";
  63. tenant.IsLocked = false;
  64. uow.Commit();
  65. WriteInfo($"Tenant: '{tenant.Code}' created");
  66. }
  67. else
  68. {
  69. WriteCaption($"Tenant: '{tenant.Code}' already exists");
  70. }
  71. var ruser = new RepoUser(uow.As<IDataDomain>());
  72. var user = ruser.GetByLoginName("testuser");
  73. if (user == null)
  74. {
  75. user = ruser.New();
  76. user.LoginName = "testuser";
  77. tenant.IsLocked = false;
  78. uow.Commit();
  79. WriteInfo($"User: '{user.LoginName}' created");
  80. }
  81. else
  82. {
  83. WriteCaption($"User: '{user.LoginName}' already exists");
  84. }
  85. WriteInfo($"Assigning user '{user.LoginName}' to tenants..");
  86. var tenant1Id = rtenant.GetByCode("default")?.Id;
  87. var tenant2Id = rtenant.GetByCode("test")?.Id;
  88. var assign = new TenantUser();
  89. assign.UserId = user.Id;
  90. assign.TenantId = tenant1Id.GetValueOrDefault();
  91. assign = new TenantUser();
  92. assign.UserId = user.Id;
  93. assign.TenantId = tenant2Id.GetValueOrDefault();
  94. uow.Commit();
  95. WriteInfo("Assigned.");
  96. }
  97. catch (SqlException e)
  98. {
  99. return HandleConnectionException(e);
  100. }
  101. return new Result(resultCode);
  102. }
  103. #endregion
  104. #region *** Private operations ***
  105. #endregion
  106. }
  107. }