TenantService.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.ComponentModel.DataAnnotations;
  2. using Quadarax.Application.QLiberace.Base.Repositories;
  3. using Quadarax.Foundation.Core.Business;
  4. using Microsoft.Extensions.Logging;
  5. using Quadarax.Application.QLiberace.Base.Dtos;
  6. using Quadarax.Application.QLiberace.Base.Dtos.Validators;
  7. using Quadarax.Application.QLiberace.Base.Entities;
  8. using Quadarax.Application.QLiberace.Base.Mapper;
  9. using Quadarax.Foundation.Core.Data.Interface.Domain;
  10. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  11. using Quadarax.Application.QLiberace.Common;
  12. using Quadarax.Application.QLiberace.Common.Domain;
  13. using Quadarax.Foundation.Core.Logging;
  14. using Quadarax.Foundation.Core.Value;
  15. namespace Quadarax.Application.QLiberace.Base.Services
  16. {
  17. public class TenantService : AbstractRepositoryService<RepoTenant>
  18. {
  19. #region *** Constructor ***
  20. public TenantService(RepoTenant repository, IContext currentContext, IQLogger logger) : base(repository, currentContext, logger)
  21. {
  22. }
  23. #endregion
  24. #region *** Public Operations ***
  25. public ResultBoolDto ExistsTenant(string tenantCode)
  26. {
  27. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  28. return TryCatchBlock(() =>
  29. {
  30. var result = Repository.GetByCode(tenantCode);
  31. if (result !=null) UpdateAccess(result);
  32. return new ResultBoolDto(result!=null);
  33. });
  34. }
  35. public ResultDto Lock(string tenantCode, bool isLocked)
  36. {
  37. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  38. return TryCatchBlock(() =>
  39. {
  40. var result = Repository.GetByCode(tenantCode);
  41. if (result == null) throw
  42. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode);
  43. result.IsLocked = isLocked;
  44. Repository.Set(result);
  45. UpdateAccess(result);
  46. return new ResultPlain();
  47. });
  48. }
  49. public ResultValueDto<TenantRDto?> GetTenant(string tenantCode)
  50. {
  51. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  52. return TryCatchBlock(() =>
  53. {
  54. var result = Repository.GetByCode(tenantCode);
  55. if (result == null) throw
  56. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode);
  57. UpdateAccess(result);
  58. return new ResultValueDto<TenantRDto?>(result.Map<Tenant, TenantRDto>());
  59. });
  60. }
  61. public ResultValueDto<TenantRDto?> GetTenant(long tenantId)
  62. {
  63. return TryCatchBlock(() =>
  64. {
  65. var result = Repository.Get(tenantId);
  66. if (result == null) throw
  67. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString());
  68. UpdateAccess(result);
  69. return new ResultValueDto<TenantRDto?>(result.Map<Tenant, TenantRDto>());
  70. });
  71. }
  72. public ResultValueDto<TenantRDto?> CreateTenant(TenantWDto tenant)
  73. {
  74. if (tenant==null) throw new ArgumentNullException(nameof(tenant));
  75. return TryCatchBlock(() =>
  76. {
  77. ValidateWDto(tenant);
  78. var exists = Repository.GetByCode(tenant.Code);
  79. if (exists != null) throw
  80. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantAlreadyExistsCode, tenant.Code);
  81. var tenantDao = Repository.New();
  82. tenant.CopyToDto(tenantDao);
  83. return new ResultValueDto<TenantRDto?>(tenantDao.Map<Tenant, TenantRDto>());
  84. });
  85. }
  86. public ResultPlain Validate(string tenantCode)
  87. {
  88. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  89. return TryCatchBlock(() =>
  90. {
  91. var result = Repository.GetByCode(tenantCode);
  92. if (result == null) throw
  93. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode);
  94. if(result.IsLocked) throw
  95. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantLockedCode, tenantCode);
  96. UpdateAccess(result);
  97. return new ResultPlain();
  98. });
  99. }
  100. public ResultDto UpdateTenant(TenantWDto tenant)
  101. {
  102. if (tenant==null) throw new ArgumentNullException(nameof(tenant));
  103. return TryCatchBlock(() =>
  104. {
  105. ValidateWDto(tenant);
  106. var exists = Repository.GetByCode(tenant.Code);
  107. if (exists == null) throw
  108. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenant.Code);
  109. tenant.CopyToDto(exists);
  110. Repository.Set(exists);
  111. UpdateAccess(exists);
  112. return new ResultPlain();
  113. });
  114. }
  115. public ResultDto DeleteTenant(long tenantId)
  116. {
  117. return TryCatchBlock(() =>
  118. {
  119. var result = Repository.Get(tenantId);
  120. if (result == null) throw
  121. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString());
  122. Repository.Remove(tenantId);
  123. return new ResultPlain();
  124. });
  125. }
  126. #endregion
  127. #region *** Private Operations ***
  128. private void UpdateAccess(Tenant tenant)
  129. {
  130. tenant.LastAccess = DateTime.Now;
  131. Repository.Set(tenant);
  132. }
  133. private void ValidateWDto(TenantWDto tenant)
  134. {
  135. if (tenant == null) throw new ArgumentNullException(nameof(tenant));
  136. var validator = new TenantValidator();
  137. validator.Validate(tenant, new ValidatorContext());
  138. if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException());
  139. }
  140. private void ValidateSameTenant(TenantWDto tenant)
  141. {
  142. if (tenant == null) throw new ArgumentNullException(nameof(tenant));
  143. var currentTenantCode = CurrentContext.GetContext<QlbrcContext>().TenantCode;
  144. if (!string.Equals(currentTenantCode, tenant.Code, StringComparison.InvariantCulture))
  145. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantDifferentContextCode, tenant.Code, currentTenantCode!);
  146. }
  147. #endregion
  148. }
  149. }