UserTaskService.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 
  2. using MTWorkHR.Application.Models;
  3. using MTWorkHR.Core.UnitOfWork;
  4. using MTWorkHR.Application.Services.Interfaces;
  5. using MTWorkHR.Core.Entities;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Core.Global;
  8. using Microsoft.EntityFrameworkCore;
  9. using System.Linq.Dynamic.Core;
  10. namespace MTWorkHR.Application.Services
  11. {
  12. public class UserTaskService : BaseService<UserTask, UserTaskDto, UserTaskDto>, IUserTaskService
  13. {
  14. private readonly IUnitOfWork _unitOfWork;
  15. //private readonly AppSettingsConfiguration _configuration;
  16. //private readonly GlobalInfo _globalInfo;
  17. private readonly IFileService _fileService;
  18. private readonly GlobalInfo _globalInfo;
  19. public UserTaskService(IUnitOfWork unitOfWork, IFileService fileService, GlobalInfo globalInfo) : base(unitOfWork)
  20. {
  21. _unitOfWork = unitOfWork;
  22. _fileService = fileService;
  23. _globalInfo = globalInfo;
  24. }
  25. public override async Task<UserTaskDto> GetById(long id)
  26. {
  27. var entity = await _unitOfWork.UserTask.GetByIdWithAllChildren(id);
  28. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  29. return response;
  30. }
  31. public async Task<UserTaskDto> GetByUserId(string userId)
  32. {
  33. var entity = await _unitOfWork.UserTask.GetByUserIdWithAllChildren(userId);
  34. var response = MapperObject.Mapper.Map<UserTaskDto>(entity);
  35. return response;
  36. }
  37. public override async Task<PagingResultDto<UserTaskDto>> GetAll(PagingInputDto PagingInputDto)
  38. {
  39. var res = await _unitOfWork.UserTask.GetAllWithChildrenAsync();
  40. var query = res.Item1;
  41. if (_globalInfo.UserType != UserTypeEnum.Business)
  42. {
  43. query = query.Where(m => m.AssignedUserId == _globalInfo.UserId);
  44. }
  45. if (PagingInputDto.Filter != null)
  46. {
  47. var filter = PagingInputDto.Filter;
  48. query = query.Where(u => u.Title.Contains(filter) || u.Description.Contains(filter));
  49. }
  50. var order = query.OrderBy(PagingInputDto.OrderByField + " " + PagingInputDto.OrderType);
  51. var page = order.Skip((PagingInputDto.PageNumber * PagingInputDto.PageSize) - PagingInputDto.PageSize).Take(PagingInputDto.PageSize);
  52. var total = await query.CountAsync();
  53. var list = MapperObject.Mapper
  54. .Map<IList<UserTaskDto>>(await page.ToListAsync());
  55. var response = new PagingResultDto<UserTaskDto>
  56. {
  57. Result = list,
  58. Total = total
  59. };
  60. return response;
  61. }
  62. public override async Task<UserTaskDto> Create(UserTaskDto input)
  63. {
  64. if(input.TaskAttachments?.Count > 0)
  65. if ( !await _fileService.CopyFileToActualFolder(input.TaskAttachments.ToList()))
  66. throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  67. var entity = MapperObject.Mapper.Map<UserTask>(input);
  68. if (entity is null)
  69. {
  70. throw new AppException(ExceptionEnum.MapperIssue);
  71. }
  72. var task = await _unitOfWork.UserTask.AddAsync(entity);
  73. await _unitOfWork.CompleteAsync();
  74. var response = MapperObject.Mapper.Map<UserTaskDto>(task);
  75. return response;
  76. }
  77. public override async Task<UserTaskDto> Update(UserTaskDto input)
  78. {
  79. var entitiy = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
  80. if (entitiy == null)
  81. throw new AppException(ExceptionEnum.RecordNotExist);
  82. MapperObject.Mapper.Map(input, entitiy, typeof(UserTaskDto), typeof(UserTask));
  83. await _unitOfWork.CompleteAsync();
  84. return input;
  85. }
  86. }
  87. }