No Description

CommandeService.cs 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using AutoMapper;
  2. using Backend.ApplicationCore.Interfaces.IRepositories;
  3. using Backend.ApplicationCore.Interfaces.IServices;
  4. using Backend.Domain;
  5. using Backend.Domain.Entities;
  6. using Backend.Models;
  7. using Microsoft.Extensions.Logging;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Reflection;
  11. using System.Threading.Tasks;
  12. namespace Backend.ApplicationCore.Services
  13. {
  14. public class CommandeService: ICommandeService
  15. {
  16. private readonly ICommandeRepository _CommandeRepository;
  17. private readonly ILogger<CommandeService> _log;
  18. private readonly IMapper _mapper;
  19. public CommandeService(ILogger<CommandeService> log,
  20. IMapper mapper,
  21. ICommandeRepository CommandeRepository)
  22. {
  23. _log = log;
  24. _mapper = mapper;
  25. _CommandeRepository = CommandeRepository;
  26. }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <returns></returns>
  31. public async Task<IEnumerable<CommandeResponse>> GetAllAsync()
  32. {
  33. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Get all Type");
  34. if (_CommandeRepository != null)
  35. {
  36. using Task<IEnumerable<Commande>> task = _CommandeRepository.FindAllAsync();
  37. await task;
  38. if (task.IsCompleted && task.IsCompletedSuccessfully)
  39. {
  40. return _mapper.Map<IEnumerable<CommandeResponse>>(task.Result);
  41. }
  42. else
  43. {
  44. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR +": Type"}");
  45. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR +": Type");
  46. }
  47. }
  48. else
  49. {
  50. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  51. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  52. }
  53. }
  54. /// <summary>
  55. ///
  56. /// </summary>
  57. /// <param name="commandeDto"></param>
  58. /// <returns></returns>
  59. public async Task<int> CreateOrUpdateAsync(CommandeDto commandeDto)
  60. {
  61. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Create or Update Type");
  62. if (_CommandeRepository != null && commandeDto != null)
  63. {
  64. Commande currentCommande = _mapper.Map<Commande>(commandeDto);
  65. if (commandeDto.Id != null)
  66. {
  67. using Task<Commande> taskFind = _CommandeRepository.FindAsync(commandeDto.Id);
  68. await taskFind;
  69. if (taskFind.IsCompleted && taskFind.IsCompletedSuccessfully)
  70. {
  71. if (taskFind.Result != null)
  72. {
  73. currentCommande.DateEnregistrement = taskFind.Result.DateEnregistrement;
  74. _CommandeRepository.Entry(currentCommande, taskFind.Result);
  75. using Task<Commande> result = _CommandeRepository.UpdateAsync(currentCommande);
  76. await result;
  77. if (result.IsCompleted && result.IsCompletedSuccessfully)
  78. {
  79. using Task<int> taskUpdate = _CommandeRepository.SaveChangesAsync();
  80. await taskUpdate;
  81. if (taskUpdate.IsCompleted && taskUpdate.IsCompletedSuccessfully)
  82. {
  83. return taskUpdate.Result;
  84. }
  85. else
  86. {
  87. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande"}");
  88. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande");
  89. }
  90. }
  91. else
  92. {
  93. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": Commande"}");
  94. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": Commande");
  95. }
  96. }
  97. else
  98. {
  99. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": Commande"}");
  100. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  101. }
  102. }
  103. else
  104. {
  105. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProriete"}");
  106. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProprite");
  107. }
  108. }
  109. else
  110. {
  111. currentCommande.Id = new Guid();
  112. currentCommande.DateEnregistrement = DateTime.Now.Date;
  113. using Task<Commande> result = _CommandeRepository.AddAsync(currentCommande);
  114. await result;
  115. if (result.IsCompleted && result.IsCompletedSuccessfully)
  116. {
  117. using Task<int> taskAdd = _CommandeRepository.SaveChangesAsync();
  118. await taskAdd;
  119. if (taskAdd.IsCompleted && taskAdd.IsCompletedSuccessfully)
  120. {
  121. return taskAdd.Result;
  122. }
  123. else
  124. {
  125. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande"}");
  126. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande");
  127. }
  128. }
  129. else
  130. {
  131. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": Commande"}");
  132. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": Commande");
  133. }
  134. }
  135. }
  136. else
  137. {
  138. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  139. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  140. }
  141. }
  142. /// <summary>
  143. ///
  144. /// </summary>
  145. /// <param name="Id"></param>
  146. /// <returns></returns>
  147. public async Task<int> DeleteByIdAsync(Guid Id)
  148. {
  149. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to delete Type");
  150. if (_CommandeRepository != null && Id != null)
  151. {
  152. using Task<Commande> task = _CommandeRepository.FindAsync(Id);
  153. await task;
  154. if (task.IsCompleted && task.IsCompletedSuccessfully)
  155. {
  156. if (task.Result != null)
  157. {
  158. using Task taskDelete = _CommandeRepository.DeleteAsync(task.Result);
  159. await taskDelete;
  160. if (taskDelete.IsCompleted && taskDelete.IsCompletedSuccessfully)
  161. {
  162. using Task<int> taskSave = _CommandeRepository.SaveChangesAsync();
  163. await taskSave;
  164. if (taskSave.IsCompleted && taskSave.IsCompletedSuccessfully)
  165. {
  166. return taskSave.Result;
  167. }
  168. else
  169. {
  170. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande"}");
  171. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Commande");
  172. }
  173. }
  174. else
  175. {
  176. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": Commande"}");
  177. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": Commande");
  178. }
  179. }
  180. else
  181. {
  182. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": Type"}");
  183. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  184. }
  185. }
  186. else
  187. {
  188. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": Type"}");
  189. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": Type");
  190. }
  191. }
  192. else
  193. {
  194. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  195. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  196. }
  197. }
  198. }
  199. }

Powered by TurnKey Linux.