Ejemplo n.º 1
0
        private async Task UpdateParticipants(TeamViewModel teamViewModel, int managerId)
        {
            List <Participant> participants = null;

            participants = await ParticipantRepository.All.Where(p => p.TeamId == teamViewModel.Id).ToListAsync();

            // if the team didn't have any participants before, just map all participants over
            if (participants.Count == 0)
            {
                // map any player information
                foreach (ParticipantViewModel participant in teamViewModel.Players)
                {
                    Participant newParticipant = null;
                    try
                    {
                        newParticipant = Mapper.Map <Participant>(participant);
                    }
                    catch (Exception e)
                    {
                        var exceptionMessage = e.Message;
                    }
                    newParticipant.TeamId = teamViewModel.Id;
                    ParticipantRepository.InsertOrUpdate(newParticipant);
                }
            }
            else
            {
                // if the team had participants, update/add participant information
                foreach (ParticipantViewModel participant in teamViewModel.Players)
                {
                    var existingParticipant = participants.Find(p => p.Id == participant.Id);
                    // if found, then update
                    if (existingParticipant != null)
                    {
                        Mapper.Map(participant, existingParticipant);
                        existingParticipant.TeamId = existingParticipant.Team.Id;
                        ParticipantRepository.InsertOrUpdate(existingParticipant);
                    }
                    else // insert new participant
                    {
                        var newParticipant = Mapper.Map <Participant>(participant);
                        newParticipant.TeamId = teamViewModel.Id;
                        ParticipantRepository.InsertOrUpdate(newParticipant);
                    }
                }
                // check for deleted players
                foreach (Participant participant in participants)
                {
                    if (teamViewModel.Players.Count(p => p.Id == participant.Id) == 0)
                    {
                        participant.IsDeleted = true;
                        participant.DeletedOn = DateTime.Now;
                        participant.DeletedBy = managerId;
                        ParticipantRepository.InsertOrUpdate(participant);
                    }
                }
            }
        }