UserTaskService.cs 4.0 KB

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