|
@@ -21,34 +21,69 @@ namespace MTWorkHR.Application.Services
|
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
|
//private readonly AppSettingsConfiguration _configuration;
|
|
|
//private readonly GlobalInfo _globalInfo;
|
|
|
+ private readonly ITeamService _teamService;
|
|
|
|
|
|
- public ProjectService(IUnitOfWork unitOfWork):base(unitOfWork)
|
|
|
+ public ProjectService(IUnitOfWork unitOfWork, ITeamService teamService) : base(unitOfWork)
|
|
|
{
|
|
|
_unitOfWork = unitOfWork;
|
|
|
+ _teamService = teamService;
|
|
|
}
|
|
|
|
|
|
+ public override async Task<ProjectDto> GetById(long id)
|
|
|
+ {
|
|
|
+ var entity = await _unitOfWork.Project.GetByIdWithAllChildren(id);
|
|
|
+ var response = MapperObject.Mapper.Map<ProjectDto>(entity);
|
|
|
+ if (response != null)
|
|
|
+ response.ProjectTeamIds = response.ProjectTeams != null ? response.ProjectTeams?.Select(u => u.TeamId).ToList() : new List<long>();
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ public override async Task<ProjectDto> Create(ProjectDto input)
|
|
|
+ {
|
|
|
+ var entity = MapperObject.Mapper.Map<Project>(input);
|
|
|
+
|
|
|
+ if (entity is null)
|
|
|
+ {
|
|
|
+ throw new AppException(ExceptionEnum.MapperIssue);
|
|
|
+ }
|
|
|
+ entity.ProjectTeams = input.ProjectTeamIds?.Select(s => new ProjectTeam { TeamId = s, TeamName = _teamService.GetTeamName(s).Result }).ToList();
|
|
|
|
|
|
- //public override async Task<ProjectDto> GetById(long id)
|
|
|
- //{
|
|
|
- // var entity = await _unitOfWork.Project.GetByIdAsync(id);
|
|
|
- // var response = MapperObject.Mapper.Map<ProjectDto>(entity);
|
|
|
- // return response;
|
|
|
- //}
|
|
|
+ var Project = await _unitOfWork.Project.AddAsync(entity);
|
|
|
+ await _unitOfWork.CompleteAsync();
|
|
|
|
|
|
- //public override async Task<List<ProjectDto>> GetAll()
|
|
|
- //{
|
|
|
- // var projects = await _unitOfWork.Project.GetAllAsync();
|
|
|
- // var response = MapperObject.Mapper.Map<List<ProjectDto>>(projects);
|
|
|
- // return response;
|
|
|
- //}
|
|
|
+ var response = MapperObject.Mapper.Map<ProjectDto>(Project);
|
|
|
+ response.ProjectTeamIds = input.ProjectTeamIds;
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public override async Task<ProjectDto> Update(ProjectDto input)
|
|
|
+ {
|
|
|
+ var entity = await _unitOfWork.Project.GetByIdWithAllChildren(input.Id);
|
|
|
+ if (entity is null)
|
|
|
+ throw new AppException(ExceptionEnum.RecordNotExist);
|
|
|
+ entity.NameAr = input.NameAr;
|
|
|
+ entity.NameEn = input.NameEn;
|
|
|
+ var oldUsers = entity.ProjectTeams?.Select(s => s.TeamId);
|
|
|
|
|
|
- //public override async Task Delete(long id)
|
|
|
- //{
|
|
|
- // var entity = await _unitOfWork.Project.GetByIdAsync(id);
|
|
|
- // await _unitOfWork.Project.DeleteAsync(entity);
|
|
|
- //}
|
|
|
+ var NewItems = input.ProjectTeamIds?.Where(u => !oldUsers.Contains(u));
|
|
|
+ var newProjectTeams = NewItems?.Select(s => new ProjectTeam { ProjectId = input.Id, TeamId = s, TeamName = _teamService.GetTeamName(s).Result}).ToList();
|
|
|
+ var DeletedItems = oldUsers?.Where(u => !input.ProjectTeamIds.Contains(u));
|
|
|
+ _unitOfWork.BeginTran();
|
|
|
+ foreach (var delUser in DeletedItems)
|
|
|
+ {
|
|
|
+ var removeItem = entity.ProjectTeams?.FirstOrDefault(u => u.TeamId == delUser);
|
|
|
+ if (removeItem != null) await _unitOfWork.ProjectTeam.DeleteAsync(removeItem);
|
|
|
+ }
|
|
|
+ foreach (var newteam in newProjectTeams)
|
|
|
+ {
|
|
|
+ await _unitOfWork.ProjectTeam.AddAsync(newteam);
|
|
|
+ }
|
|
|
|
|
|
+ await _unitOfWork.CompleteAsync();
|
|
|
+ _unitOfWork.CommitTran();
|
|
|
|
|
|
+ var response = Mapper.MapperObject.Mapper.Map<ProjectDto>(entity);
|
|
|
+ return response;
|
|
|
+ }
|
|
|
|
|
|
}
|
|
|
}
|