TenantService.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Quadarax.Application.QLiberace.Base.Repositories;
  2. using Quadarax.Foundation.Core.Business;
  3. using Microsoft.Extensions.Logging;
  4. using Quadarax.Application.QLiberace.Base.Dtos;
  5. using Quadarax.Application.QLiberace.Base.Entities;
  6. using Quadarax.Application.QLiberace.Base.Mapper;
  7. using Quadarax.Foundation.Core.Data.Interface.Domain;
  8. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  9. using Quadarax.Application.QLiberace.Common;
  10. namespace Quadarax.Application.QLiberace.Base.Services
  11. {
  12. public class TenantService : AbstractRepositoryService<RepoTenant>
  13. {
  14. #region *** Constructor ***
  15. public TenantService(RepoTenant repository, IContext currentContext, ILoggerFactory logger) : base(repository, currentContext, logger)
  16. {
  17. }
  18. #endregion
  19. #region *** Public Operations ***
  20. public ResultBoolDto ExistsTenant(string tenantCode)
  21. {
  22. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  23. return TryCatchBlock(() =>
  24. {
  25. var result = Repository.GetByCode(tenantCode);
  26. if (result !=null) UpdateAccess(result);
  27. return new ResultBoolDto(result!=null);
  28. });
  29. }
  30. public ResultDto Lock(string tenantCode, bool isLocked)
  31. {
  32. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  33. return TryCatchBlock(() =>
  34. {
  35. var result = Repository.GetByCode(tenantCode);
  36. if (result == null) throw
  37. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode);
  38. result.IsLocked = isLocked;
  39. Repository.Set(result);
  40. UpdateAccess(result);
  41. return new ResultPlain();
  42. });
  43. }
  44. public ResultValueDto<TenantRDto?> GetTenant(string tenantCode)
  45. {
  46. if (string.IsNullOrEmpty(tenantCode)) throw new ArgumentNullException(nameof(tenantCode));
  47. return TryCatchBlock(() =>
  48. {
  49. var result = Repository.GetByCode(tenantCode);
  50. if (result == null) throw
  51. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenantCode);
  52. UpdateAccess(result);
  53. return new ResultValueDto<TenantRDto?>(result.Map<Tenant, TenantRDto>());
  54. });
  55. }
  56. public ResultValueDto<TenantRDto?> GetTenant(long tenantId)
  57. {
  58. return TryCatchBlock(() =>
  59. {
  60. var result = Repository.Get(tenantId);
  61. if (result == null) throw
  62. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString());
  63. UpdateAccess(result);
  64. return new ResultValueDto<TenantRDto?>(result.Map<Tenant, TenantRDto>());
  65. });
  66. }
  67. public ResultValueDto<TenantRDto?> CreateTenant(TenantWDto tenant)
  68. {
  69. if (tenant==null) throw new ArgumentNullException(nameof(tenant));
  70. return TryCatchBlock(() =>
  71. {
  72. var exists = Repository.GetByCode(tenant.Code);
  73. if (exists != null) throw
  74. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantAlreadyExistsCode, tenant.Code);
  75. var tenantDao = Repository.New();
  76. tenant.CopyToDto(tenantDao);
  77. return new ResultValueDto<TenantRDto?>(tenantDao.Map<Tenant, TenantRDto>());
  78. });
  79. }
  80. public ResultDto UpdateTenant(TenantWDto tenant)
  81. {
  82. if (tenant==null) throw new ArgumentNullException(nameof(tenant));
  83. return TryCatchBlock(() =>
  84. {
  85. var exists = Repository.GetByCode(tenant.Code);
  86. if (exists == null) throw
  87. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "code", tenant.Code);
  88. tenant.CopyToDto(exists);
  89. Repository.Set(exists);
  90. UpdateAccess(exists);
  91. return new ResultPlain();
  92. });
  93. }
  94. public ResultDto DeleteTenant(long tenantId)
  95. {
  96. return TryCatchBlock(() =>
  97. {
  98. var result = Repository.Get(tenantId);
  99. if (result == null) throw
  100. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantNotFoundCode, "id", tenantId.ToString());
  101. Repository.Remove(tenantId);
  102. return new ResultPlain();
  103. });
  104. }
  105. #endregion
  106. #region *** Private Operations ***
  107. private void UpdateAccess(Tenant tenant)
  108. {
  109. tenant.LastAccess = DateTime.Now;
  110. Repository.Set(tenant);
  111. }
  112. #endregion
  113. }
  114. }