CatalogueService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Security.Principal;
  5. using System.Threading.Tasks;
  6. using BO.AppServer.Business.Exceptions;
  7. using BO.AppServer.Business.Mapper;
  8. using BO.AppServer.Business.Services.Base;
  9. using BO.AppServer.Data.Entity;
  10. using BO.AppServer.Metadata.Configuration;
  11. using BO.AppServer.Metadata.Dto;
  12. using BO.AppServer.Metadata.Enums;
  13. using Microsoft.AspNetCore.Identity;
  14. using Microsoft.Extensions.Logging;
  15. using Microsoft.Extensions.Options;
  16. using Quadarax.Foundation.Core.Data;
  17. using Quadarax.Foundation.Core.Data.Interface;
  18. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  19. namespace BO.AppServer.Business.Services
  20. {
  21. public class CatalogueService : UserContextService
  22. {
  23. #region *** Private fields ***
  24. private readonly MimeTypeRepo _repoMimeType;
  25. private readonly BillingPlanRepo _repoBillingPlan;
  26. #endregion
  27. #region *** Constructors ***
  28. public CatalogueService(
  29. MimeTypeRepo repoMimeType,
  30. BillingPlanRepo repoBillingPlan,
  31. UserRepo repoUser,
  32. UserManager<IdentityUser> userManager,
  33. IOptions<Configuration> config,
  34. IPrincipal currentPrincipal, ILoggerFactory logger) : base(config, repoUser, userManager, currentPrincipal, logger)
  35. {
  36. _repoMimeType = repoMimeType ?? throw new ArgumentNullException(nameof(repoMimeType));
  37. _repoBillingPlan = repoBillingPlan ?? throw new ArgumentNullException(nameof(repoBillingPlan));
  38. }
  39. #endregion
  40. #region *** Public operations ***
  41. #region **** MIME Types ****
  42. public async Task<ResultsValueDto<MimeTypeRDto>> GetMimeTypesAsync(MimeTypeScopeEnums scope, PagingDto paging)
  43. {
  44. List<MimeType> mimeTypes = null;
  45. switch (scope)
  46. {
  47. case MimeTypeScopeEnums.All:
  48. mimeTypes = _repoMimeType.Query(paging).ToList();
  49. break;
  50. }
  51. return new ResultsValueDto<MimeTypeRDto>(mimeTypes.MapList<MimeType, MimeTypeRDto>());
  52. }
  53. public async Task<ResultValueDto<MimeTypeRDto>> CreateMimeTypeAsync(MimeTypeCDto mimeType, bool isSilent = false)
  54. {
  55. // Validation
  56. var exists = _repoMimeType.Query(new Paging())
  57. .Any(x => x.IsEnabled && x.Mimetype1 == mimeType.Mimetype1.ToLower());
  58. if (exists)
  59. {
  60. var exc = new EntityAlreadyExistsException(typeof(MimeType), mimeType.Mimetype1);
  61. Log.LogWarning(exc.Message);
  62. if (!isSilent)
  63. throw exc;
  64. }
  65. // Create entity
  66. var newMimeType = _repoMimeType.New();
  67. mimeType.CopyToDto(newMimeType);
  68. newMimeType.Mimetype1 = newMimeType.Mimetype1.ToLower();
  69. newMimeType.Created = DateTime.Now;
  70. newMimeType.CreatedByUserId = GetCurrentUserId();
  71. newMimeType.IsEnabled = true;
  72. _repoMimeType.Set(newMimeType);
  73. _repoMimeType.Commit();
  74. return new ResultValueDto<MimeTypeRDto>(newMimeType.Map<MimeType, MimeTypeRDto>());
  75. }
  76. public async Task<ResultValueDto<MimeTypeRDto>> UpdateMimeTypeAsync(MimeTypeUDto mimeType)
  77. {
  78. // Validation
  79. var updMimeType = _repoMimeType.Get(mimeType.Id);
  80. if (updMimeType == null)
  81. {
  82. var exc = new EntityNotExistsException(typeof(MimeType), mimeType.Mimetype1, mimeType.Id);
  83. Log.LogWarning(exc.Message);
  84. throw exc;
  85. }
  86. // Update entity
  87. mimeType.CopyToDto(updMimeType);
  88. updMimeType.Mimetype1 = updMimeType.Mimetype1.ToLower();
  89. updMimeType.Modified = DateTime.Now;
  90. updMimeType.ModifiedByUser = GetCurrentUser();
  91. _repoMimeType.Set(updMimeType);
  92. _repoMimeType.Commit();
  93. return new ResultValueDto<MimeTypeRDto>(updMimeType.Map<MimeType, MimeTypeRDto>());
  94. }
  95. #endregion
  96. #region **** Billing Plan ****
  97. public async Task<ResultsValueDto<BillingPlanRDto>> GetBillingPlansAsync(BillingPlanScopeEnums scope, PagingDto paging)
  98. {
  99. List<BillingPlan> billingPlans = null;
  100. switch (scope)
  101. {
  102. case BillingPlanScopeEnums.All:
  103. billingPlans = _repoBillingPlan.Query(paging).ToList();
  104. break;
  105. }
  106. return new ResultsValueDto<BillingPlanRDto>(billingPlans.MapList<BillingPlan, BillingPlanRDto>());
  107. }
  108. public async Task<ResultValueDto<BillingPlanRDto>> CreateBillingPlanAsync(BillingPlanCDto billingPlan, bool isSilent = false)
  109. {
  110. // Validation
  111. var exists = _repoBillingPlan.Query(new Paging())
  112. .Any(x => x.IsEnabled && x.Code == billingPlan.Code.ToLower());
  113. if (exists)
  114. {
  115. var exc = new EntityAlreadyExistsException(typeof(BillingPlan), billingPlan.Code);
  116. Log.LogWarning(exc.Message);
  117. if (!isSilent)
  118. throw exc;
  119. }
  120. if (billingPlan.Code.Length > 10)
  121. {
  122. var exc = new DataSizeValidationException(typeof(BillingPlan), nameof(BillingPlanCDto.Code), 10);
  123. Log.LogWarning(exc.Message);
  124. if (!isSilent)
  125. throw exc;
  126. }
  127. if (billingPlan.PstprocessProfile.Length > 5)
  128. {
  129. var exc = new DataSizeValidationException(typeof(BillingPlan), nameof(BillingPlanCDto.PstprocessProfile), 5);
  130. Log.LogWarning(exc.Message);
  131. if (!isSilent)
  132. throw exc;
  133. }
  134. // Create entity
  135. var newBillingPlan = _repoBillingPlan.New();
  136. billingPlan.Code = billingPlan.Code.ToUpper();
  137. billingPlan.CopyToDto(newBillingPlan);
  138. newBillingPlan.Created = DateTime.Now;
  139. newBillingPlan.CreatedByUser = GetCurrentUser();
  140. newBillingPlan.IsEnabled = true;
  141. _repoBillingPlan.Set(newBillingPlan);
  142. _repoBillingPlan.Commit();
  143. return new ResultValueDto<BillingPlanRDto>(newBillingPlan.Map<BillingPlan, BillingPlanRDto>());
  144. }
  145. public async Task<ResultValueDto<BillingPlanRDto>> UpdateBillingPlanAsync(BillingPlanUDto billingPlan)
  146. {
  147. // Validation
  148. var updBillingPlan = _repoBillingPlan.Get(billingPlan.Id);
  149. if (updBillingPlan == null)
  150. {
  151. var exc = new EntityNotExistsException(typeof(BillingPlan), billingPlan.Code, billingPlan.Id);
  152. Log.LogWarning(exc.Message);
  153. throw exc;
  154. }
  155. // Update entity
  156. billingPlan.CopyToDto(updBillingPlan);
  157. updBillingPlan.Modified = DateTime.Now;
  158. updBillingPlan.ModifiedByUser = GetCurrentUser();
  159. _repoMimeType.Commit();
  160. return new ResultValueDto<BillingPlanRDto>(updBillingPlan.Map<BillingPlan, BillingPlanRDto>());
  161. }
  162. #endregion
  163. #endregion
  164. #region *** Private operations ***
  165. #endregion
  166. }
  167. }