BaseService.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. 
  2. using MTWorkHR.Application.Models;
  3. using MTWorkHR.Application.Services.Interfaces;
  4. using MTWorkHR.Core.Entities.Base;
  5. using MTWorkHR.Core.Global;
  6. using MTWorkHR.Core.IRepositories.Base;
  7. using MTWorkHR.Core.UnitOfWork;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace MTWorkHR.Application.Services
  12. {
  13. public class BaseService<TEntity, TDto, TGetAllDto> : IService<TEntity, TDto, TGetAllDto>
  14. where TEntity : class
  15. where TDto : EntityDto
  16. where TGetAllDto : class
  17. {
  18. private readonly IUnitOfWork unitOfWork;
  19. private readonly IRepository<TEntity> repository;
  20. public BaseService(IUnitOfWork _unitOfWork)
  21. {
  22. unitOfWork = _unitOfWork;
  23. repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
  24. }
  25. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  26. {
  27. var result = await repository.GetAllAsync(pagingInputDto);
  28. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  29. var response = new PagingResultDto<TGetAllDto>
  30. {
  31. Result = list,
  32. Total = result.Item2
  33. };
  34. return response;
  35. }
  36. public virtual async Task<TDto> GetById(long id)
  37. {
  38. var entity = await repository.GetByIdAsync(id);
  39. var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
  40. return response;
  41. }
  42. public virtual async Task Delete(long id)
  43. {
  44. var entity = await repository.GetByIdAsync(id);
  45. if (entity == null)
  46. throw new AppException(ExceptionEnum.RecordNotExist);
  47. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  48. if (isSoftDelete)
  49. {
  50. ((ISoftDelete)entity).IsDeleted = true;
  51. }
  52. else
  53. {
  54. await repository.DeleteAsync(entity);
  55. }
  56. await unitOfWork.CompleteAsync();
  57. }
  58. public virtual async Task<TDto> Create(TDto input)
  59. {
  60. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  61. if (employeeEntitiy is null)
  62. {
  63. throw new AppException(ExceptionEnum.MapperIssue);
  64. }
  65. var newEmployee = await repository.AddAsync(employeeEntitiy);
  66. await unitOfWork.CompleteAsync();
  67. var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
  68. return response;
  69. }
  70. public virtual async Task<TDto> Update(TDto input)
  71. {
  72. var entitiy = await repository.GetByIdAsync(input.Id);
  73. if (entitiy == null)
  74. throw new AppException(ExceptionEnum.RecordNotExist);
  75. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
  76. await unitOfWork.CompleteAsync();
  77. return input;
  78. }
  79. }
  80. public class BaseService<TEntity, TDto, TUpdatDto, TGetAllDto> : IService<TEntity, TDto, TUpdatDto, TGetAllDto>
  81. where TEntity : class
  82. where TDto : EntityDto
  83. where TUpdatDto : EntityDto
  84. where TGetAllDto : class
  85. {
  86. private readonly IUnitOfWork unitOfWork;
  87. private readonly IRepository<TEntity> repository;
  88. public BaseService(IUnitOfWork _unitOfWork)
  89. {
  90. unitOfWork = _unitOfWork;
  91. repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
  92. }
  93. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  94. {
  95. var result = await repository.GetAllAsync();
  96. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  97. var response = new PagingResultDto<TGetAllDto>
  98. {
  99. Result = list,
  100. Total = result.Item2
  101. };
  102. return response;
  103. }
  104. public virtual async Task<TDto> GetById(long id)
  105. {
  106. var entity = await repository.GetByIdAsync(id);
  107. var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
  108. return response;
  109. }
  110. public virtual async Task Delete(long id)
  111. {
  112. var entity = await repository.GetByIdAsync(id);
  113. if (entity == null)
  114. throw new AppException(ExceptionEnum.RecordNotExist);
  115. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  116. if (isSoftDelete)
  117. {
  118. ((ISoftDelete)entity).IsDeleted = true;
  119. }
  120. else
  121. {
  122. await repository.DeleteAsync(entity);
  123. }
  124. await unitOfWork.CompleteAsync();
  125. }
  126. public virtual async Task<TDto> Create(TDto input)
  127. {
  128. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  129. if (employeeEntitiy is null)
  130. {
  131. throw new AppException(ExceptionEnum.MapperIssue);
  132. }
  133. var newEmployee = await repository.AddAsync(employeeEntitiy);
  134. await unitOfWork.CompleteAsync();
  135. var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
  136. return response;
  137. }
  138. public virtual async Task<TDto> Update(TUpdatDto input)
  139. {
  140. var entitiy = await repository.GetByIdAsync(input.Id);
  141. if (entitiy == null)
  142. throw new AppException(ExceptionEnum.RecordNotExist);
  143. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
  144. await unitOfWork.CompleteAsync();
  145. var response = Mapper.MapperObject.Mapper.Map<TDto>(entitiy);
  146. return response;
  147. }
  148. }
  149. public class BaseService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto> : IService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto>
  150. where TEntity : class
  151. where TGetDto : EntityDto
  152. where TCreateDto : EntityDto
  153. where TUpdatDto : EntityDto
  154. where TGetAllDto : class
  155. {
  156. private readonly IUnitOfWork unitOfWork;
  157. private readonly IRepository<TEntity> repository;
  158. public BaseService(IUnitOfWork _unitOfWork)
  159. {
  160. unitOfWork = _unitOfWork;
  161. repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
  162. }
  163. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  164. {
  165. var result = await repository.GetAllAsync();
  166. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  167. var response = new PagingResultDto<TGetAllDto>
  168. {
  169. Result = list,
  170. Total = result.Item2
  171. };
  172. return response;
  173. }
  174. public virtual async Task<TGetDto> GetById(long id)
  175. {
  176. var entity = await repository.GetByIdAsync(id);
  177. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entity);
  178. return response;
  179. }
  180. public virtual async Task Delete(long id)
  181. {
  182. var entity = await repository.GetByIdAsync(id);
  183. if (entity == null)
  184. throw new AppException(ExceptionEnum.RecordNotExist);
  185. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  186. if (isSoftDelete)
  187. {
  188. ((ISoftDelete)entity).IsDeleted = true;
  189. }
  190. else
  191. {
  192. await repository.DeleteAsync(entity);
  193. }
  194. await unitOfWork.CompleteAsync();
  195. }
  196. public virtual async Task<TGetDto> Create(TCreateDto input)
  197. {
  198. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  199. if (employeeEntitiy is null)
  200. {
  201. throw new AppException(ExceptionEnum.MapperIssue);
  202. }
  203. var newEmployee = await repository.AddAsync(employeeEntitiy);
  204. await unitOfWork.CompleteAsync();
  205. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(newEmployee);
  206. return response;
  207. }
  208. public virtual async Task<TGetDto> Update(TUpdatDto input)
  209. {
  210. var entitiy = await repository.GetByIdAsync(input.Id);
  211. if (entitiy == null)
  212. throw new AppException(ExceptionEnum.RecordNotExist);
  213. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TGetDto), typeof(TEntity));
  214. await unitOfWork.CompleteAsync();
  215. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entitiy);
  216. return response;
  217. }
  218. }
  219. }