BaseService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
  26. //{
  27. // throw new NotImplementedException();
  28. //}
  29. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  30. {
  31. var result = await repository.GetAllAsync();
  32. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  33. var response = new PagingResultDto<TGetAllDto>
  34. {
  35. Result = list,
  36. Total = result.Item2
  37. };
  38. return response;
  39. }
  40. public virtual async Task<TDto> GetById(long id)
  41. {
  42. var entity = await repository.GetByIdAsync(id);
  43. var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
  44. return response;
  45. }
  46. public virtual async Task Delete(long id)
  47. {
  48. var entity = await repository.GetByIdAsync(id);
  49. if (entity == null)
  50. throw new AppException(ExceptionEnum.RecordNotExist);
  51. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  52. if (isSoftDelete)
  53. {
  54. ((ISoftDelete)entity).IsDeleted = true;
  55. }
  56. else
  57. {
  58. await repository.DeleteAsync(entity);
  59. }
  60. await unitOfWork.CompleteAsync();
  61. }
  62. public virtual async Task<TDto> Create(TDto input)
  63. {
  64. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  65. if (employeeEntitiy is null)
  66. {
  67. throw new AppException(ExceptionEnum.MapperIssue);
  68. }
  69. var newEmployee = await repository.AddAsync(employeeEntitiy);
  70. await unitOfWork.CompleteAsync();
  71. var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
  72. return response;
  73. }
  74. public virtual async Task<TDto> Update(TDto input)
  75. {
  76. var entitiy = await repository.GetByIdAsync(input.Id);
  77. if (entitiy == null)
  78. throw new AppException(ExceptionEnum.RecordNotExist);
  79. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
  80. await unitOfWork.CompleteAsync();
  81. return input;
  82. }
  83. }
  84. public class BaseService<TEntity, TDto, TUpdatDto, TGetAllDto> : IService<TEntity, TDto, TUpdatDto, TGetAllDto>
  85. where TEntity : class
  86. where TDto : EntityDto
  87. where TUpdatDto : EntityDto
  88. where TGetAllDto : class
  89. {
  90. private readonly IUnitOfWork unitOfWork;
  91. private readonly IRepository<TEntity> repository;
  92. public BaseService(IUnitOfWork _unitOfWork)
  93. {
  94. unitOfWork = _unitOfWork;
  95. repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
  96. }
  97. //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
  98. //{
  99. // throw new NotImplementedException();
  100. //}
  101. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  102. {
  103. var result = await repository.GetAllAsync();
  104. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  105. var response = new PagingResultDto<TGetAllDto>
  106. {
  107. Result = list,
  108. Total = result.Item2
  109. };
  110. return response;
  111. }
  112. public virtual async Task<TDto> GetById(long id)
  113. {
  114. var entity = await repository.GetByIdAsync(id);
  115. var response = Mapper.MapperObject.Mapper.Map<TDto>(entity);
  116. return response;
  117. }
  118. public virtual async Task Delete(long id)
  119. {
  120. var entity = await repository.GetByIdAsync(id);
  121. if (entity == null)
  122. throw new AppException(ExceptionEnum.RecordNotExist);
  123. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  124. if (isSoftDelete)
  125. {
  126. ((ISoftDelete)entity).IsDeleted = true;
  127. }
  128. else
  129. {
  130. await repository.DeleteAsync(entity);
  131. }
  132. await unitOfWork.CompleteAsync();
  133. }
  134. public virtual async Task<TDto> Create(TDto input)
  135. {
  136. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  137. if (employeeEntitiy is null)
  138. {
  139. throw new AppException(ExceptionEnum.MapperIssue);
  140. }
  141. var newEmployee = await repository.AddAsync(employeeEntitiy);
  142. await unitOfWork.CompleteAsync();
  143. var response = Mapper.MapperObject.Mapper.Map<TDto>(newEmployee);
  144. return response;
  145. }
  146. public virtual async Task<TDto> Update(TUpdatDto input)
  147. {
  148. var entitiy = await repository.GetByIdAsync(input.Id);
  149. if (entitiy == null)
  150. throw new AppException(ExceptionEnum.RecordNotExist);
  151. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TDto), typeof(TEntity));
  152. await unitOfWork.CompleteAsync();
  153. var response = Mapper.MapperObject.Mapper.Map<TDto>(entitiy);
  154. return response;
  155. }
  156. }
  157. public class BaseService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto> : IService<TEntity, TGetDto, TCreateDto, TUpdatDto, TGetAllDto>
  158. where TEntity : class
  159. where TGetDto : EntityDto
  160. where TCreateDto : EntityDto
  161. where TUpdatDto : EntityDto
  162. where TGetAllDto : class
  163. {
  164. private readonly IUnitOfWork unitOfWork;
  165. private readonly IRepository<TEntity> repository;
  166. public BaseService(IUnitOfWork _unitOfWork)
  167. {
  168. unitOfWork = _unitOfWork;
  169. repository = (IRepository<TEntity>)unitOfWork.GetRepositoryByName(typeof(TEntity).Name);
  170. }
  171. //Task<IPagingResultDto<TGetAllDto>> IService<TEntity, TDto, TGetAllDto>.GetAll(int pageNumber, int pageSize, string orderByField, string orderType)
  172. //{
  173. // throw new NotImplementedException();
  174. //}
  175. public virtual async Task<PagingResultDto<TGetAllDto>> GetAll(PagingInputDto pagingInputDto)
  176. {
  177. var result = await repository.GetAllAsync();
  178. var list = Mapper.MapperObject.Mapper.Map<IList<TGetAllDto>>(result.Item1);
  179. var response = new PagingResultDto<TGetAllDto>
  180. {
  181. Result = list,
  182. Total = result.Item2
  183. };
  184. return response;
  185. }
  186. public virtual async Task<TGetDto> GetById(long id)
  187. {
  188. var entity = await repository.GetByIdAsync(id);
  189. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entity);
  190. return response;
  191. }
  192. public virtual async Task Delete(long id)
  193. {
  194. var entity = await repository.GetByIdAsync(id);
  195. if (entity == null)
  196. throw new AppException(ExceptionEnum.RecordNotExist);
  197. var isSoftDelete = typeof(TEntity).GetInterfaces().Any(x => x == typeof(ISoftDelete));
  198. if (isSoftDelete)
  199. {
  200. ((ISoftDelete)entity).IsDeleted = true;
  201. }
  202. else
  203. {
  204. await repository.DeleteAsync(entity);
  205. }
  206. await unitOfWork.CompleteAsync();
  207. }
  208. public virtual async Task<TGetDto> Create(TCreateDto input)
  209. {
  210. var employeeEntitiy = Mapper.MapperObject.Mapper.Map<TEntity>(input);
  211. if (employeeEntitiy is null)
  212. {
  213. throw new AppException(ExceptionEnum.MapperIssue);
  214. }
  215. var newEmployee = await repository.AddAsync(employeeEntitiy);
  216. await unitOfWork.CompleteAsync();
  217. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(newEmployee);
  218. return response;
  219. }
  220. public virtual async Task<TGetDto> Update(TUpdatDto input)
  221. {
  222. var entitiy = await repository.GetByIdAsync(input.Id);
  223. if (entitiy == null)
  224. throw new AppException(ExceptionEnum.RecordNotExist);
  225. Mapper.MapperObject.Mapper.Map(input, entitiy, typeof(TGetDto), typeof(TEntity));
  226. await unitOfWork.CompleteAsync();
  227. var response = Mapper.MapperObject.Mapper.Map<TGetDto>(entitiy);
  228. return response;
  229. }
  230. }
  231. }