CustomerService.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using Microsoft.Extensions.Logging;
  2. using Quadarax.Application.QLiberace.Base.Dtos;
  3. using Quadarax.Application.QLiberace.Base.Entities;
  4. using Quadarax.Application.QLiberace.Base.Mapper;
  5. using Quadarax.Application.QLiberace.Base.Repositories;
  6. using Quadarax.Application.QLiberace.Common;
  7. using Quadarax.Application.QLiberace.Common.Domain;
  8. using Quadarax.Application.QLiberace.Customer.Dtos;
  9. using Quadarax.Application.QLiberace.Customer.Dtos.Validators;
  10. using Quadarax.Application.QLiberace.Customer.Entities;
  11. using Quadarax.Application.QLiberace.Customer.Enums;
  12. using Quadarax.Application.QLiberace.Customer.Repositories;
  13. using Quadarax.Foundation.Core.Business;
  14. using Quadarax.Foundation.Core.Data.Interface.Domain;
  15. using Quadarax.Foundation.Core.Data.Interface.Entity.Dto;
  16. using Quadarax.Foundation.Core.Value;
  17. namespace Quadarax.Application.QLiberace.Customer.Services
  18. {
  19. public class CustomerService : AbstractMultiRepositoryService<RepoCustomer,RepoContact, RepoUser, RepoCountry>
  20. {
  21. #region *** Constructor ***
  22. public CustomerService(RepoCustomer repository1, RepoContact repository2, RepoUser repository3, RepoCountry repository4, IContext currentContext, ILoggerFactory logger) : base(repository1, repository2, repository3, repository4, currentContext, logger)
  23. {
  24. }
  25. #endregion
  26. #region *** Public operations ***
  27. #region **** Customer ****
  28. public ResultValueDto<CustomerRDto?> CreateCustomer(CustomerWDto customer)
  29. {
  30. if (customer==null) throw new ArgumentNullException(nameof(customer));
  31. return TryCatchBlock(() =>
  32. {
  33. ValidateWDto(customer);
  34. var customerDao = Repository1.New();
  35. customer.CopyToDto(customerDao);
  36. return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
  37. });
  38. }
  39. public ResultDto UpdateCustomer(string customerCode, CustomerWDto customer)
  40. {
  41. if (customer==null) throw new ArgumentNullException(nameof(customer));
  42. return TryCatchBlock(() =>
  43. {
  44. ValidateWDto(customer);
  45. var customerDao = FetchCustomer(customerCode);
  46. customer.CopyToDto(customerDao);
  47. return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
  48. });
  49. }
  50. public ResultDto DeleteCustomer(long customerId)
  51. {
  52. return TryCatchBlock(() =>
  53. {
  54. var result = FetchCustomer(customerId);
  55. Repository1.Remove(customerId);
  56. return new ResultPlain();
  57. });
  58. }
  59. public ResultValueDto<CustomerRDto?> GetCustomer(string customerCode)
  60. {
  61. return TryCatchBlock(() =>
  62. {
  63. var customerDao = FetchCustomer(customerCode);
  64. return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
  65. });
  66. }
  67. public ResultValueDto<CustomerRDto?> GetCustomer(long customerId)
  68. {
  69. return TryCatchBlock(() =>
  70. {
  71. var customerDao = FetchCustomer(customerId);
  72. return new ResultValueDto<CustomerRDto?>(customerDao.Map<Entities.Customer, CustomerRDto>());
  73. });
  74. }
  75. public ResultBoolDto ExistsCustomer(string customerCode)
  76. {
  77. return TryCatchBlock(() => new ResultBoolDto(FetchCustomer(customerCode)!=null));
  78. }
  79. #endregion
  80. #region **** User Assignments ****
  81. public ResultDto AssignUser(string customerCode, string userLogin)
  82. {
  83. return TryCatchBlock(() =>
  84. {
  85. var customerDao = FetchCustomer(customerCode);
  86. FetchUser(userLogin);
  87. if (customerDao!.Users.Any(x=>string.Equals(x.UserLoginName, userLogin)))
  88. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerUserAlreadyAssignedCode, "code",customerCode, userLogin);
  89. customerDao.Users.Add(new CustomerUser()
  90. {
  91. CustomerId = customerDao.Id,
  92. UserLoginName = userLogin
  93. });
  94. return new ResultPlain();
  95. });
  96. }
  97. public ResultDto UnAssignUser(string customerCode, string userLogin)
  98. {
  99. return TryCatchBlock(() =>
  100. {
  101. var customerDao = FetchCustomer(customerCode);
  102. FetchUser(userLogin);
  103. var customerUser = customerDao!.Users.FirstOrDefault(x => string.Equals(x.UserLoginName, userLogin));
  104. if (customerUser == null)
  105. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerUserNotFoundAssignedCode, "code",customerCode, userLogin);
  106. customerDao.Users.Remove(customerUser);
  107. return new ResultPlain();
  108. });
  109. }
  110. #endregion
  111. #region **** Customer Constacts ****
  112. public ResultValueDto<ContactRDto?> CreateContact(string customerCode, ContactWDto contact)
  113. {
  114. if (contact==null) throw new ArgumentNullException(nameof(contact));
  115. return TryCatchBlock(() =>
  116. {
  117. var customerDao = FetchCustomer(customerCode);
  118. ValidateWDto(contact);
  119. var contactDao = Repository2.New();
  120. contact.CopyToDto(contactDao);
  121. return new ResultValueDto<ContactRDto?>(contactDao.Map<Contact, ContactRDto>());
  122. });
  123. }
  124. public ResultDto UpdateContact(string customerCode, ContactWDto contact)
  125. {
  126. throw new NotImplementedException();
  127. }
  128. public ResultDto DeleteContact(long contactId)
  129. {
  130. throw new NotImplementedException();
  131. }
  132. public ResultValueDto<ContactRDto?> GetContact(long contactId)
  133. {
  134. throw new NotImplementedException();
  135. }
  136. public ResultsValueDto<ContactRDto?> GetContacts(string customerCode)
  137. {
  138. throw new NotImplementedException();
  139. }
  140. public ResultsValueDto<ContactRDto?> GetPreferredContacts(string customerCode, params ContactTypeEnum[] contactTypes)
  141. {
  142. throw new NotImplementedException();
  143. }
  144. public ResultDto ValidateContacts(string customerCode)
  145. {
  146. throw new NotImplementedException();
  147. }
  148. #endregion
  149. #endregion
  150. #region *** Private Operations ***
  151. private void ValidateWDto(CustomerWDto customer)
  152. {
  153. if (customer == null) throw new ArgumentNullException(nameof(customer));
  154. var validator = new CustomerValidator();
  155. validator.Validate(customer, new ValidatorContext(new KeyValuePair<string, object>[]{ new("RepoCountry", Repository4)}));
  156. if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException());
  157. }
  158. private void ValidateWDto(ContactWDto contact)
  159. {
  160. if (contact == null) throw new ArgumentNullException(nameof(contact));
  161. var validator = new ContactValidator();
  162. validator.Validate(contact, new ValidatorContext(new KeyValuePair<string, object>[]{ new("RepoCountry", Repository4)}));
  163. if (!validator.IsSuccess) throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.ValidationErrorsCode, validator.ToAggregateException());
  164. }
  165. private void ValidateSameTenant(TenantWDto tenant)
  166. {
  167. if (tenant == null) throw new ArgumentNullException(nameof(tenant));
  168. var currentTenantCode = CurrentContext.GetContext<QlbrcContext>().TenantCode;
  169. if (!string.Equals(currentTenantCode, tenant.Code, StringComparison.InvariantCulture))
  170. throw ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.TenantDifferentContextCode, tenant.Code, currentTenantCode!);
  171. }
  172. private Entities.Customer? FetchCustomer(string customerCode)
  173. {
  174. if (string.IsNullOrEmpty(customerCode)) throw new ArgumentNullException(nameof(customerCode));
  175. var exists = Repository1.GetByCode(customerCode);
  176. if (exists != null) throw
  177. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerNotFoundCode, "code", customerCode);
  178. return exists;
  179. }
  180. private User? FetchUser(string userLogin)
  181. {
  182. if (string.IsNullOrEmpty(userLogin)) throw new ArgumentNullException(nameof(userLogin));
  183. var exists = Repository3.GetByLoginName(userLogin);
  184. if (exists != null) throw
  185. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.UserNotFoundCode, "loginName", userLogin);
  186. return exists;
  187. }
  188. private Entities.Customer? FetchCustomer(long customerId)
  189. {
  190. var exists = Repository1.Get(customerId);
  191. if (exists != null) throw
  192. ExceptionFactory.CreateException(ExceptionLibrary.ExceptionsCodes.CustomerNotFoundCode, "id", customerId.ToString());
  193. return exists;
  194. }
  195. #endregion
  196. }
  197. }