Nav apraksta

LigneCommandeService.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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.Linq;
  11. using System.Reflection;
  12. using System.Threading.Tasks;
  13. namespace Backend.ApplicationCore.Services
  14. {
  15. public class LigneCommandeService: ILigneCommandeService
  16. {
  17. private readonly ILigneCommandeRepository _LigneCommandeRepository;
  18. private readonly ILogger<LigneCommandeService> _log;
  19. private readonly IMapper _mapper;
  20. public LigneCommandeService(ILogger<LigneCommandeService> log,
  21. IMapper mapper,
  22. ILigneCommandeRepository LigneCommandeRepository)
  23. {
  24. _log = log;
  25. _mapper = mapper;
  26. _LigneCommandeRepository = LigneCommandeRepository;
  27. }
  28. /// <summary>
  29. ///
  30. /// </summary>
  31. /// <returns></returns>
  32. public async Task<IEnumerable<LigneCommandeResponse>> GetAllAsync()
  33. {
  34. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Get all Type");
  35. if (_LigneCommandeRepository != null)
  36. {
  37. using Task<IEnumerable<LigneCommande>> task = _LigneCommandeRepository.FindAllAsync();
  38. await task;
  39. if (task.IsCompleted && task.IsCompletedSuccessfully)
  40. {
  41. return _mapper.Map<IEnumerable<LigneCommandeResponse>>(task.Result);
  42. }
  43. else
  44. {
  45. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR +": Type"}");
  46. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR +": Type");
  47. }
  48. }
  49. else
  50. {
  51. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  52. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  53. }
  54. }
  55. /// <summary>
  56. ///
  57. /// </summary>
  58. /// <param name="ligneCommandeDto"></param>
  59. /// <returns></returns>
  60. public async Task<int> CreateOrUpdateAsync(LigneCommandeDto ligneCommandeDto)
  61. {
  62. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Create or Update Type");
  63. if (_LigneCommandeRepository != null && ligneCommandeDto != null)
  64. {
  65. LigneCommande currentLigneCommande = _mapper.Map<LigneCommande>(ligneCommandeDto);
  66. if (ligneCommandeDto.Id != null)
  67. {
  68. using Task<LigneCommande> taskFind = _LigneCommandeRepository.FindAsync(ligneCommandeDto.Id);
  69. await taskFind;
  70. if (taskFind.IsCompleted && taskFind.IsCompletedSuccessfully)
  71. {
  72. if (taskFind.Result != null)
  73. {
  74. currentLigneCommande.Prix = taskFind.Result.Prix;
  75. _LigneCommandeRepository.Entry(currentLigneCommande, taskFind.Result);
  76. using Task<LigneCommande> result = _LigneCommandeRepository.UpdateAsync(currentLigneCommande);
  77. await result;
  78. if (result.IsCompleted && result.IsCompletedSuccessfully)
  79. {
  80. using Task<int> taskUpdate = _LigneCommandeRepository.SaveChangesAsync();
  81. await taskUpdate;
  82. if (taskUpdate.IsCompleted && taskUpdate.IsCompletedSuccessfully)
  83. {
  84. return taskUpdate.Result;
  85. }
  86. else
  87. {
  88. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": LigneCommande"}");
  89. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  90. }
  91. }
  92. else
  93. {
  94. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": LigneCommande"}");
  95. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": LigneCommande");
  96. }
  97. }
  98. else
  99. {
  100. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": LigneCommande"}");
  101. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  102. }
  103. }
  104. else
  105. {
  106. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProriete"}");
  107. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProprite");
  108. }
  109. }
  110. else
  111. {
  112. await InitialyseDataBeforSaveAsync(currentLigneCommande);
  113. using Task<LigneCommande> result = _LigneCommandeRepository.AddAsync(currentLigneCommande);
  114. await result;
  115. if (result.IsCompleted && result.IsCompletedSuccessfully)
  116. {
  117. using Task<int> taskAdd = _LigneCommandeRepository.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 + ": LigneCommande"}");
  126. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  127. }
  128. }
  129. else
  130. {
  131. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": LigneCommande"}");
  132. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": LigneCommande");
  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="ligneCommandeDto"></param>
  146. /// <returns></returns>
  147. private async Task InitialyseDataBeforSaveAsync(LigneCommande ligneCommandeDto)
  148. {
  149. ligneCommandeDto.Id = new Guid();
  150. using Task<decimal> x = GetPrice(_LigneCommandeRepository.GetLigneCommandesOfDate(DateTime.Now.Date));
  151. await x;
  152. if (x.IsCompleted && x.IsCompletedSuccessfully)
  153. {
  154. ligneCommandeDto.Prix = x.Result;
  155. }
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. /// <param name="Id"></param>
  161. /// <returns></returns>
  162. public async Task<int> DeleteByIdAsync(Guid Id)
  163. {
  164. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to delete Type");
  165. if (_LigneCommandeRepository != null && Id != null)
  166. {
  167. using Task<LigneCommande> task = _LigneCommandeRepository.FindAsync(Id);
  168. await task;
  169. if (task.IsCompleted && task.IsCompletedSuccessfully)
  170. {
  171. if (task.Result != null)
  172. {
  173. using Task taskDelete = _LigneCommandeRepository.DeleteAsync(task.Result);
  174. await taskDelete;
  175. if (taskDelete.IsCompleted && taskDelete.IsCompletedSuccessfully)
  176. {
  177. using Task<int> taskSave = _LigneCommandeRepository.SaveChangesAsync();
  178. await taskSave;
  179. if (taskSave.IsCompleted && taskSave.IsCompletedSuccessfully)
  180. {
  181. return taskSave.Result;
  182. }
  183. else
  184. {
  185. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": LigneCommande"}");
  186. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  187. }
  188. }
  189. else
  190. {
  191. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": LigneCommande"}");
  192. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": LigneCommande");
  193. }
  194. }
  195. else
  196. {
  197. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": Type"}");
  198. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  199. }
  200. }
  201. else
  202. {
  203. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": Type"}");
  204. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": Type");
  205. }
  206. }
  207. else
  208. {
  209. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  210. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  211. }
  212. }
  213. public async Task<decimal> GetPrice(List<LigneCommande> ligneCommandes)
  214. {
  215. if (ligneCommandes != null)
  216. {
  217. int compteur = ligneCommandes.Count();
  218. if (compteur > 1)
  219. {
  220. return ligneCommandes[0].Prix + ligneCommandes[1].Prix;
  221. }
  222. else
  223. {
  224. return 1;
  225. }
  226. }
  227. else
  228. {
  229. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG + ": Ligne Commande"}");
  230. throw new Exception(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG + ": Ligne Commande");
  231. }
  232. }
  233. }
  234. }

Powered by TurnKey Linux.