ProjectService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using Microsoft.AspNetCore.Identity;
  2. using Microsoft.AspNetCore.WebUtilities;
  3. using Microsoft.EntityFrameworkCore;
  4. using Microsoft.Extensions.Configuration;
  5. using MTWorkHR.Application.Identity;
  6. using MTWorkHR.Application.Mapper;
  7. using MTWorkHR.Application.Models;
  8. using MTWorkHR.Core.Global;
  9. using MTWorkHR.Core.IRepositories;
  10. using MTWorkHR.Core.UnitOfWork;
  11. using MTWorkHR.Infrastructure.Entities;
  12. using MTWorkHR.Application.Services.Interfaces;
  13. using MTWorkHR.Core.Email;
  14. using MTWorkHR.Core.Entities;
  15. using MTWorkHR.Infrastructure.UnitOfWorks;
  16. namespace MTWorkHR.Application.Services
  17. {
  18. public class ProjectService : BaseService<Project, ProjectDto, ProjectDto>, IProjectService
  19. {
  20. private readonly IUnitOfWork _unitOfWork;
  21. //private readonly AppSettingsConfiguration _configuration;
  22. //private readonly GlobalInfo _globalInfo;
  23. private readonly ITeamService _teamService;
  24. public ProjectService(IUnitOfWork unitOfWork, ITeamService teamService) : base(unitOfWork)
  25. {
  26. _unitOfWork = unitOfWork;
  27. _teamService = teamService;
  28. }
  29. public override async Task<ProjectDto> GetById(long id)
  30. {
  31. var entity = await _unitOfWork.Project.GetByIdWithAllChildren(id);
  32. var response = MapperObject.Mapper.Map<ProjectDto>(entity);
  33. if (response != null)
  34. response.ProjectTeamIds = response.ProjectTeams != null ? response.ProjectTeams?.Select(u => u.TeamId).ToList() : new List<long>();
  35. return response;
  36. }
  37. public override async Task<ProjectDto> Create(ProjectDto input)
  38. {
  39. var entity = MapperObject.Mapper.Map<Project>(input);
  40. if (entity is null)
  41. {
  42. throw new AppException(ExceptionEnum.MapperIssue);
  43. }
  44. entity.ProjectTeams = input.ProjectTeamIds?.Select(s => new ProjectTeam { TeamId = s, TeamName = _teamService.GetTeamName(s).Result }).ToList();
  45. var Project = await _unitOfWork.Project.AddAsync(entity);
  46. await _unitOfWork.CompleteAsync();
  47. var response = MapperObject.Mapper.Map<ProjectDto>(Project);
  48. response.ProjectTeamIds = input.ProjectTeamIds;
  49. return response;
  50. }
  51. public override async Task<ProjectDto> Update(ProjectDto input)
  52. {
  53. var entity = await _unitOfWork.Project.GetByIdWithAllChildren(input.Id);
  54. if (entity is null)
  55. throw new AppException(ExceptionEnum.RecordNotExist);
  56. entity.NameAr = input.NameAr;
  57. entity.NameEn = input.NameEn;
  58. var oldUsers = entity.ProjectTeams?.Select(s => s.TeamId);
  59. var NewItems = input.ProjectTeamIds?.Where(u => !oldUsers.Contains(u));
  60. var newProjectTeams = NewItems?.Select(s => new ProjectTeam { ProjectId = input.Id, TeamId = s, TeamName = _teamService.GetTeamName(s).Result}).ToList();
  61. var DeletedItems = oldUsers?.Where(u => !input.ProjectTeamIds.Contains(u));
  62. _unitOfWork.BeginTran();
  63. foreach (var delUser in DeletedItems)
  64. {
  65. var removeItem = entity.ProjectTeams?.FirstOrDefault(u => u.TeamId == delUser);
  66. if (removeItem != null) await _unitOfWork.ProjectTeam.DeleteAsync(removeItem);
  67. }
  68. foreach (var newteam in newProjectTeams)
  69. {
  70. await _unitOfWork.ProjectTeam.AddAsync(newteam);
  71. }
  72. await _unitOfWork.CompleteAsync();
  73. _unitOfWork.CommitTran();
  74. var response = Mapper.MapperObject.Mapper.Map<ProjectDto>(entity);
  75. return response;
  76. }
  77. }
  78. }