TeamService.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 async Task<bool> AssignAdminManager(TeamUserDto teamUser)
  32. {
  33. var result = false;
  34. var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
  35. if (team?.TeamUsers != null)
  36. {
  37. var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
  38. if (foundUser != null)
  39. {
  40. foundUser.IsAdmin = teamUser.IsAdmin;
  41. }
  42. else
  43. {
  44. team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
  45. }
  46. }
  47. else if (team != null)
  48. {
  49. team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
  50. }
  51. await _unitOfWork.CompleteAsync();
  52. return result;
  53. }
  54. }
  55. }