No Description

ProprieteService.cs 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 ProprieteService: IProprieteService
  15. {
  16. private readonly IProprieteRepository _ProprieteRepository;
  17. private readonly ILogger<ProprieteService> _log;
  18. private readonly IMapper _mapper;
  19. public ProprieteService(ILogger<ProprieteService> log,
  20. IMapper mapper,
  21. IProprieteRepository ProprieteRepository)
  22. {
  23. _log = log;
  24. _mapper = mapper;
  25. _ProprieteRepository = ProprieteRepository;
  26. }
  27. /// <summary>
  28. ///
  29. /// </summary>
  30. /// <returns></returns>
  31. public async Task<IEnumerable<ProprieteResponse>> GetAllAsync()
  32. {
  33. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Get all propriete");
  34. if (_ProprieteRepository != null)
  35. {
  36. using Task<IEnumerable<Propriete>> task = _ProprieteRepository.FindAllAsync();
  37. await task;
  38. if (task.IsCompleted && task.IsCompletedSuccessfully)
  39. {
  40. return _mapper.Map<IEnumerable<ProprieteResponse>>(task.Result);
  41. }
  42. else
  43. {
  44. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR +": propriete"}");
  45. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR +": propriete");
  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="ProprieteDto"></param>
  58. /// <returns></returns>
  59. public async Task<int> CreateOrUpdateAsync(ProprieteDto ProprieteDto)
  60. {
  61. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to Create or Update propriete");
  62. if (_ProprieteRepository != null && ProprieteDto != null)
  63. {
  64. if (ProprieteDto.Id != null)
  65. {
  66. using Task<Propriete> taskFind = _ProprieteRepository.FindAsync(ProprieteDto.Id);
  67. await taskFind;
  68. if (taskFind.IsCompleted && taskFind.IsCompletedSuccessfully)
  69. {
  70. if (taskFind.Result != null)
  71. {
  72. _ProprieteRepository.Entry(_mapper.Map<Propriete>(ProprieteDto), taskFind.Result);
  73. using Task<Propriete> result = _ProprieteRepository.UpdateAsync(_mapper.Map<Propriete>(ProprieteDto));
  74. await result;
  75. if (result.IsCompleted && result.IsCompletedSuccessfully)
  76. {
  77. using Task<int> taskUpdate = _ProprieteRepository.SaveChangesAsync();
  78. await taskUpdate;
  79. if (taskUpdate.IsCompleted && taskUpdate.IsCompletedSuccessfully)
  80. {
  81. return taskUpdate.Result;
  82. }
  83. else
  84. {
  85. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete"}");
  86. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete");
  87. }
  88. }
  89. else
  90. {
  91. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": Propriete"}");
  92. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_UPDATE + ": Propriete");
  93. }
  94. }
  95. else
  96. {
  97. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": Propriete"}");
  98. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  99. }
  100. }
  101. else
  102. {
  103. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProriete"}");
  104. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": TypeProprite");
  105. }
  106. }
  107. else
  108. {
  109. ProprieteDto.Id = new Guid();
  110. using Task<Propriete> result = _ProprieteRepository.AddAsync(_mapper.Map<Propriete>(ProprieteDto));
  111. await result;
  112. if (result.IsCompleted && result.IsCompletedSuccessfully)
  113. {
  114. using Task<int> taskAdd = _ProprieteRepository.SaveChangesAsync();
  115. await taskAdd;
  116. if (taskAdd.IsCompleted && taskAdd.IsCompletedSuccessfully)
  117. {
  118. return taskAdd.Result;
  119. }
  120. else
  121. {
  122. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete"}");
  123. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete");
  124. }
  125. }
  126. else
  127. {
  128. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": Propriete"}");
  129. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_ADD + ": Propriete");
  130. }
  131. }
  132. }
  133. else
  134. {
  135. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  136. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  137. }
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. /// <param name="Id"></param>
  143. /// <returns></returns>
  144. public async Task<int> DeleteByIdAsync(Guid Id)
  145. {
  146. _log.LogDebug($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - request to delete Type");
  147. if (_ProprieteRepository != null && Id != null)
  148. {
  149. using Task<Propriete> task = _ProprieteRepository.FindAsync(Id);
  150. await task;
  151. if (task.IsCompleted && task.IsCompletedSuccessfully)
  152. {
  153. if (task.Result != null)
  154. {
  155. using Task taskDelete = _ProprieteRepository.DeleteAsync(task.Result);
  156. await taskDelete;
  157. if (taskDelete.IsCompleted && taskDelete.IsCompletedSuccessfully)
  158. {
  159. using Task<int> taskSave = _ProprieteRepository.SaveChangesAsync();
  160. await taskSave;
  161. if (taskSave.IsCompleted && taskSave.IsCompletedSuccessfully)
  162. {
  163. return taskSave.Result;
  164. }
  165. else
  166. {
  167. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete"}");
  168. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_SAVE + ": Propriete");
  169. }
  170. }
  171. else
  172. {
  173. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": Propriete"}");
  174. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR_REMOVE + ": Propriete");
  175. }
  176. }
  177. else
  178. {
  179. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_NOT_EXIST_MSG_ERROR + ": Type"}");
  180. throw new ArgumentException(ErrorsConstants.S_NOT_EXIST_MSG_ERROR);
  181. }
  182. }
  183. else
  184. {
  185. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_REPO_MSG_ERROR + ": Type"}");
  186. throw new Exception(ErrorsConstants.S_REPO_MSG_ERROR + ": Type");
  187. }
  188. }
  189. else
  190. {
  191. _log.LogError($"{this.GetType().Name} -> {MethodBase.GetCurrentMethod()} - {ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG}");
  192. throw new ArgumentNullException(ErrorsConstants.S_APPLICATION_DBCONTEXT_NULL_MSG);
  193. }
  194. }
  195. }
  196. }

Powered by TurnKey Linux.