No Description

LigneCommandeService.cs 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 LigneCommandeService: ILigneCommandeService
  18. {
  19. private readonly ILigneCommandeRepository _LigneCommandeRepository;
  20. private readonly ILogger<LigneCommandeService> _log;
  21. private readonly IMapper _mapper;
  22. public LigneCommandeService(ILogger<LigneCommandeService> log,
  23. IMapper mapper,
  24. ILigneCommandeRepository LigneCommandeRepository)
  25. {
  26. _log = log;
  27. _mapper = mapper;
  28. _LigneCommandeRepository = LigneCommandeRepository;
  29. }
  30. /// <summary>
  31. ///
  32. /// </summary>
  33. /// <returns></returns>
  34. public async Task<IEnumerable<LigneCommandeResponse>> GetAll()
  35. {
  36. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Get all Type");
  37. if (_LigneCommandeRepository != null)
  38. {
  39. using Task<IEnumerable<LigneCommande>> task = _LigneCommandeRepository.FindAllAsync();
  40. await task;
  41. if (task.IsCompleted && task.IsCompletedSuccessfully)
  42. {
  43. return _mapper.Map<IEnumerable<LigneCommandeResponse>>(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="ligneCommandeDto"></param>
  61. /// <returns></returns>
  62. public async Task<int> CreateOrUpdate(LigneCommandeDto ligneCommandeDto)
  63. {
  64. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Create or Update Type");
  65. if (_LigneCommandeRepository != null && ligneCommandeDto != null)
  66. {
  67. LigneCommande currentLigneCommande = _mapper.Map<LigneCommande>(ligneCommandeDto);
  68. if (ligneCommandeDto.Id != null)
  69. {
  70. using Task<LigneCommande> taskFind = _LigneCommandeRepository.FindAsync(ligneCommandeDto.Id);
  71. await taskFind;
  72. if (taskFind.IsCompleted && taskFind.IsCompletedSuccessfully)
  73. {
  74. if (taskFind.Result != null)
  75. {
  76. currentLigneCommande.Prix = taskFind.Result.Prix;
  77. _LigneCommandeRepository.Entry(currentLigneCommande, taskFind.Result);
  78. using Task<LigneCommande> result = _LigneCommandeRepository.UpdateAsync(currentLigneCommande);
  79. await result;
  80. if (result.IsCompleted && result.IsCompletedSuccessfully)
  81. {
  82. using Task<int> taskUpdate = _LigneCommandeRepository.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 + ": LigneCommande"}");
  91. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  92. }
  93. }
  94. else
  95. {
  96. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR_UPDATE + ": LigneCommande"}");
  97. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_UPDATE + ": LigneCommande");
  98. }
  99. }
  100. else
  101. {
  102. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_NOT_EXIST_MSG_ERROR + ": LigneCommande"}");
  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. await InitialyseDataBeforeSave(currentLigneCommande);
  115. using Task<LigneCommande> result = _LigneCommandeRepository.AddAsync(currentLigneCommande);
  116. await result;
  117. if (result.IsCompleted && result.IsCompletedSuccessfully)
  118. {
  119. using Task<int> taskAdd = _LigneCommandeRepository.SaveChangesAsync();
  120. await taskAdd;
  121. if (taskAdd.IsCompleted && taskAdd.IsCompletedSuccessfully)
  122. {
  123. return taskAdd.Result;
  124. }
  125. else
  126. {
  127. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR_SAVE + ": LigneCommande"}");
  128. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  129. }
  130. }
  131. else
  132. {
  133. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR_ADD + ": LigneCommande"}");
  134. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_ADD + ": LigneCommande");
  135. }
  136. }
  137. }
  138. else
  139. {
  140. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG}");
  141. throw new ArgumentNullException(ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG);
  142. }
  143. }
  144. /// <summary>
  145. ///
  146. /// </summary>
  147. /// <param name="ligneCommandeDto"></param>
  148. /// <returns></returns>
  149. private async Task InitialyseDataBeforeSave(LigneCommande ligneCommandeDto)
  150. {
  151. ligneCommandeDto.Id = new Guid();
  152. using Task<decimal> x = GetPrice(_LigneCommandeRepository.GetLigneCommandesOfDate(DateTime.Now.Date));
  153. await x;
  154. if (x.IsCompleted && x.IsCompletedSuccessfully)
  155. {
  156. ligneCommandeDto.Prix = x.Result;
  157. }
  158. }
  159. /// <summary>
  160. ///
  161. /// </summary>
  162. /// <param name="Id"></param>
  163. /// <returns></returns>
  164. public async Task<int> DeleteById(Guid Id)
  165. {
  166. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to delete Type");
  167. if (_LigneCommandeRepository != null && Id != null)
  168. {
  169. using Task<LigneCommande> task = _LigneCommandeRepository.FindAsync(Id);
  170. await task;
  171. if (task.IsCompleted && task.IsCompletedSuccessfully)
  172. {
  173. if (task.Result != null)
  174. {
  175. using Task taskDelete = _LigneCommandeRepository.DeleteAsync(task.Result);
  176. await taskDelete;
  177. if (taskDelete.IsCompleted && taskDelete.IsCompletedSuccessfully)
  178. {
  179. using Task<int> taskSave = _LigneCommandeRepository.SaveChangesAsync();
  180. await taskSave;
  181. if (taskSave.IsCompleted && taskSave.IsCompletedSuccessfully)
  182. {
  183. return taskSave.Result;
  184. }
  185. else
  186. {
  187. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR_SAVE + ": LigneCommande"}");
  188. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_SAVE + ": LigneCommande");
  189. }
  190. }
  191. else
  192. {
  193. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR_REMOVE + ": LigneCommande"}");
  194. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR_REMOVE + ": LigneCommande");
  195. }
  196. }
  197. else
  198. {
  199. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_NOT_EXIST_MSG_ERROR + ": Type"}");
  200. throw new ArgumentException(ErrorsConstants.C_NOT_EXIST_MSG_ERROR);
  201. }
  202. }
  203. else
  204. {
  205. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_REPO_MSG_ERROR + ": Type"}");
  206. throw new Exception(ErrorsConstants.C_REPO_MSG_ERROR + ": Type");
  207. }
  208. }
  209. else
  210. {
  211. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG}");
  212. throw new ArgumentNullException(ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG);
  213. }
  214. }
  215. public async Task<decimal> GetPrice(List<LigneCommande> ligneCommandes)
  216. {
  217. if (ligneCommandes != null)
  218. {
  219. int compteur = ligneCommandes.Count();
  220. if (compteur > 1)
  221. {
  222. return ligneCommandes[0].Prix + ligneCommandes[1].Prix;
  223. }
  224. else
  225. {
  226. return 1;
  227. }
  228. }
  229. else
  230. {
  231. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG + ": Ligne Commande"}");
  232. throw new Exception(ErrorsConstants.C_APPLICATION_DBCONTEXT_NULL_MSG + ": Ligne Commande");
  233. }
  234. }
  235. }
  236. }

Powered by TurnKey Linux.