No Description

CommandeService.cs 9.8KB

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

Powered by TurnKey Linux.