private async Task UpdateCommitteeDelegatesAsync(CommitteePostDto committeePostDto, Committee committee) { var newDelegatesIds = committeePostDto.Delegates.Select(x => x.Id).ToList(); var newDelegates = await _usersDbSet.Where(x => newDelegatesIds.Contains(x.Id)).ToListAsync(); committee.Delegates = newDelegates; }
private async Task <Committee> CreateCommitteeModelAsync(CommitteePostDto committeePostDto, Committee committeeModel = null) { committeeModel = _mapper.Map(committeePostDto, committeeModel) ?? _mapper.Map <CommitteePostDto, Committee>(committeePostDto); await AddRemoveCommitteeMembersAsync(committeePostDto, committeeModel); if (committeeModel != null) { await UpdateCommitteeLeadsAsync(committeePostDto, committeeModel); await UpdateCommitteeDelegatesAsync(committeePostDto, committeeModel); } return(committeeModel); }
public async Task PostCommitteeAsync(CommitteePostDto committeePostDto) { if (await _committeeRepository.GetByIdAsync(committeePostDto.Id) != null) { throw new ServiceException(Resources.Models.Committee.Committee.CommitteeIdException); } var committeeModel = await CreateCommitteeModelAsync(committeePostDto); await ChangeKudosCommitteeAsync(committeePostDto); _committeeRepository.Insert(committeeModel); await _unitOfWork.SaveAsync(); }
public async Task PutCommitteeAsync(CommitteePostDto committeePostDto) { var committeeModel = await _committeeRepository .Get(c => c.Id == committeePostDto.Id, includeProperties : "Members,Leads,Delegates") .FirstOrDefaultAsync(); if (committeeModel == null) { throw new ServiceException(Resources.Models.Committee.Committee.ModelNotFoundException); } await ChangeKudosCommitteeAsync(committeePostDto); await CreateCommitteeModelAsync(committeePostDto, committeeModel); _committeeRepository.Update(committeeModel); await _unitOfWork.SaveAsync(); }
private async Task AddRemoveCommitteeMembersAsync(CommitteePostDto committeePostDto, Committee committeeModel) { var membersInModelIds = _mapper.Map <IEnumerable <ApplicationUserMinimalDto>, string[]>(committeePostDto.Members); if (committeeModel.Members != null) { var membersInCommitteeIds = _mapper.Map <IEnumerable <ApplicationUser>, string[]>(committeeModel.Members); var membersToAdd = _applicationUserRepository.Get(u => membersInModelIds.Contains(u.Id) && !membersInCommitteeIds.Contains(u.Id)); await membersToAdd.ForEachAsync(m => committeeModel.Members.Add(m)); var membersToRemove = _applicationUserRepository.Get(u => !membersInModelIds.Contains(u.Id) && membersInCommitteeIds.Contains(u.Id)); await membersToRemove.ForEachAsync(m => committeeModel.Members.Remove(m)); } else { var membersToAdd = _applicationUserRepository.Get(u => membersInModelIds.Contains(u.Id)); committeeModel.Members = await membersToAdd.ToListAsync(); } }
private async Task ChangeKudosCommitteeAsync(CommitteePostDto committeePostDto) { if (!committeePostDto.IsKudosCommittee) { return; } var oldCommittee = await _committeeRepository.Get(k => k.IsKudosCommittee).FirstOrDefaultAsync(); if (oldCommittee == null) { return; } if (oldCommittee.Id == committeePostDto.Id) { return; } oldCommittee.IsKudosCommittee = false; _committeeRepository.Update(oldCommittee); await _unitOfWork.SaveAsync(); }