TenantService.cs 7.0 KB

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