UserTaskService.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 
  2. using MTWorkHR.Application.Models;
  3. using MTWorkHR.Core.UnitOfWork;
  4. using MTWorkHR.Application.Services.Interfaces;
  5. using MTWorkHR.Core.Entities;
  6. using Microsoft.AspNetCore.Identity;
  7. using MTWorkHR.Application.Mapper;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Infrastructure.Entities;
  10. using MTWorkHR.Infrastructure.Repositories;
  11. using MTWorkHR.Infrastructure.UnitOfWorks;
  12. using MTWorkHR.Core.IRepositories.Base;
  13. namespace MTWorkHR.Application.Services
  14. {
  15. public class UserTaskService : BaseService<UserTask, UserTaskDto, UserTaskDto>, IUserTaskService
  16. {
  17. private readonly IUnitOfWork _unitOfWork;
  18. //private readonly AppSettingsConfiguration _configuration;
  19. //private readonly GlobalInfo _globalInfo;
  20. private readonly IFileService _fileService;
  21. public UserTaskService(IUnitOfWork unitOfWork, IFileService fileService) : base(unitOfWork)
  22. {
  23. _unitOfWork = unitOfWork;
  24. _fileService = fileService;
  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(long 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<List<ProjectDto>> GetAll()
  39. //{
  40. // var projects = await _unitOfWork.Project.GetAllAsync();
  41. // var response = MapperObject.Mapper.Map<List<ProjectDto>>(projects);
  42. // return response;
  43. //}
  44. public override async Task<UserTaskDto> Create(UserTaskDto input)
  45. {
  46. if(input.TaskAttachments?.Count > 0)
  47. if ( !await _fileService.CopyFileToActualFolder(input.TaskAttachments.ToList()))
  48. throw new AppException(ExceptionEnum.CouldNotMoveFiles);
  49. var entity = MapperObject.Mapper.Map<UserTask>(input);
  50. if (entity is null)
  51. {
  52. throw new AppException(ExceptionEnum.MapperIssue);
  53. }
  54. var task = await _unitOfWork.UserTask.AddAsync(entity);
  55. await _unitOfWork.CompleteAsync();
  56. var response = MapperObject.Mapper.Map<UserTaskDto>(task);
  57. return response;
  58. }
  59. public override async Task<UserTaskDto> Update(UserTaskDto input)
  60. {
  61. var entitiy = await _unitOfWork.UserTask.GetByIdAsync(input.Id);
  62. if (entitiy == null)
  63. throw new AppException(ExceptionEnum.RecordNotExist);
  64. MapperObject.Mapper.Map(input, entitiy, typeof(UserTaskDto), typeof(UserTask));
  65. await _unitOfWork.CompleteAsync();
  66. return input;
  67. }
  68. }
  69. }