TeamService.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. using System.Linq;
  17. namespace MTWorkHR.Application.Services
  18. {
  19. public class TeamService : BaseService<Team, TeamDto, TeamDto>, ITeamService
  20. {
  21. private readonly IUnitOfWork _unitOfWork;
  22. public TeamService(IUnitOfWork unitOfWork):base(unitOfWork)
  23. {
  24. _unitOfWork = unitOfWork;
  25. }
  26. public override async Task<TeamDto> GetById(long id)
  27. {
  28. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
  29. var response = MapperObject.Mapper.Map<TeamDto>(entity);
  30. return response;
  31. }
  32. public override async Task<TeamDto> Create(TeamDto input)
  33. {
  34. var entity = MapperObject.Mapper.Map<Team>(input);
  35. if (entity is null)
  36. {
  37. throw new AppException(ExceptionEnum.MapperIssue);
  38. }
  39. entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
  40. var team = await _unitOfWork.Team.AddAsync(entity);
  41. await _unitOfWork.CompleteAsync();
  42. var response = MapperObject.Mapper.Map<TeamDto>(team);
  43. return response;
  44. }
  45. public override async Task<TeamDto> Update(TeamDto input)
  46. {
  47. var entity = await _unitOfWork.Team.GetByIdAsync(input.Id);
  48. if (entity is null)
  49. throw new AppException(ExceptionEnum.RecordNotExist);
  50. entity.NameAr = input.NameAr;
  51. entity.NameEn = input.NameEn;
  52. var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
  53. var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
  54. var newTeamUsers = NewItems?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
  55. var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
  56. foreach (var delUser in DeletedItems)
  57. {
  58. var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
  59. if(removeItem != null) entity.TeamUsers.Remove(removeItem);
  60. }
  61. foreach (var newUsr in newTeamUsers)
  62. {
  63. entity.TeamUsers.Add(newUsr);
  64. }
  65. await _unitOfWork.CompleteAsync();
  66. var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
  67. return response;
  68. }
  69. public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
  70. {
  71. var result = false;
  72. var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
  73. if (team?.TeamUsers != null)
  74. {
  75. var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
  76. if (foundUser != null)
  77. {
  78. foundUser.IsAdmin = teamUser.IsAdmin;
  79. }
  80. else
  81. {
  82. team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
  83. }
  84. }
  85. else if (team != null)
  86. {
  87. team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
  88. }
  89. await _unitOfWork.CompleteAsync();
  90. return result;
  91. }
  92. }
  93. }