No Description

ProprieteService.cs 9.7KB

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

Powered by TurnKey Linux.