CatalogueService.cs 6.9 KB

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