123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- using Microsoft.AspNetCore.Identity;
- using Microsoft.AspNetCore.WebUtilities;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Configuration;
- using MTWorkHR.Application.Identity;
- using MTWorkHR.Application.Mapper;
- using MTWorkHR.Application.Models;
- using MTWorkHR.Core.Global;
- using MTWorkHR.Core.IRepositories;
- using MTWorkHR.Core.UnitOfWork;
- using MTWorkHR.Infrastructure.Entities;
- using MTWorkHR.Application.Services.Interfaces;
- using MTWorkHR.Core.Email;
- using MTWorkHR.Core.Entities;
- using MTWorkHR.Infrastructure.UnitOfWorks;
- using System.Linq;
- namespace MTWorkHR.Application.Services
- {
- public class TeamService : BaseService<Team, TeamDto, TeamDto>, ITeamService
- {
- private readonly IUnitOfWork _unitOfWork;
-
- public TeamService(IUnitOfWork unitOfWork):base(unitOfWork)
- {
- _unitOfWork = unitOfWork;
- }
- public override async Task<TeamDto> GetById(long id)
- {
- var entity = await _unitOfWork.Team.GetByIdWithAllChildren(id);
- var response = MapperObject.Mapper.Map<TeamDto>(entity);
- return response;
- }
- public override async Task<TeamDto> Create(TeamDto input)
- {
- var entity = MapperObject.Mapper.Map<Team>(input);
-
- if (entity is null)
- {
- throw new AppException(ExceptionEnum.MapperIssue);
- }
- entity.TeamUsers = input.TeamUserIds?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
-
- var team = await _unitOfWork.Team.AddAsync(entity);
- await _unitOfWork.CompleteAsync();
- var response = MapperObject.Mapper.Map<TeamDto>(team);
- return response;
- }
- public override async Task<TeamDto> Update(TeamDto input)
- {
- var entity = await _unitOfWork.Team.GetByIdAsync(input.Id);
- if (entity is null)
- throw new AppException(ExceptionEnum.RecordNotExist);
- entity.NameAr = input.NameAr;
- entity.NameEn = input.NameEn;
- var oldUsers = entity.TeamUsers?.Select(s => s.AssignedUserId);
- var NewItems = input.TeamUserIds?.Where(u => !oldUsers.Contains(u));
- var newTeamUsers = NewItems?.Select(s => new TeamUser { AssignedUserId = s, IsAdmin = false }).ToList();
- var DeletedItems = oldUsers?.Where(u => !input.TeamUserIds.Contains(u));
- foreach (var delUser in DeletedItems)
- {
- var removeItem = entity.TeamUsers?.FirstOrDefault(u => u.AssignedUserId == delUser);
- if(removeItem != null) entity.TeamUsers.Remove(removeItem);
- }
- foreach (var newUsr in newTeamUsers)
- {
- entity.TeamUsers.Add(newUsr);
- }
- await _unitOfWork.CompleteAsync();
- var response = Mapper.MapperObject.Mapper.Map<TeamDto>(entity);
- return response;
- }
- public async Task<bool> AssignAdminManager(TeamUserDto teamUser)
- {
- var result = false;
- var team = await _unitOfWork.Team.GetByIdWithAllChildren(teamUser.TeamId);
- if (team?.TeamUsers != null)
- {
- var foundUser = team.TeamUsers.FirstOrDefault(u => u.AssignedUserId == teamUser.AssignedUserId);
- if (foundUser != null)
- {
- foundUser.IsAdmin = teamUser.IsAdmin;
- }
- else
- {
- team.TeamUsers.Add(new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId});
- }
- }
- else if (team != null)
- {
- team.TeamUsers = new List<TeamUser> { new TeamUser { IsAdmin = teamUser.IsAdmin, AssignedUserId = teamUser.AssignedUserId } };
- }
- await _unitOfWork.CompleteAsync();
-
- return result;
- }
-
- }
- }
|