TeamService.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 TeamService : BaseService<Team, TeamDto, TeamDto>, ITeamService
  19. {
  20. private readonly IUnitOfWork _unitOfWork;
  21. public TeamService(IUnitOfWork unitOfWork):base(unitOfWork)
  22. {
  23. _unitOfWork = unitOfWork;
  24. }
  25. public override async Task<TeamDto> GetById(long id)
  26. {
  27. var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
  28. var response = MapperObject.Mapper.Map<TeamDto>(entity);
  29. return response;
  30. }
  31. public override async Task<TeamDto> Create(TeamDto input)
  32. {
  33. var entity = MapperObject.Mapper.Map<Team>(input);
  34. if (entity is null)
  35. {
  36. throw new AppException(ExceptionEnum.MapperIssue);
  37. }
  38. var task = await _unitOfWork.Team.AddAsync(entity);
  39. await _unitOfWork.CompleteAsync();
  40. var response = MapperObject.Mapper.Map<TeamDto>(task);
  41. return response;
  42. }
  43. public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
  44. {
  45. var result = false;
  46. var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
  47. if (team?.TeamUsers != null)
  48. {
  49. var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
  50. if (foundUser != null)
  51. {
  52. foundUser.IsAdmin = teamUser.IsAdmin;
  53. }
  54. else
  55. {
  56. team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
  57. }
  58. }
  59. else if (team != null)
  60. {
  61. team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
  62. }
  63. await _unitOfWork.CompleteAsync();
  64. return result;
  65. }
  66. }
  67. }