CatalogueService.cs 7.2 KB

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