Пример #1
0
        public async Task UpdateSelectedOptionsAsync(EventChangeOptionsDto changeOptionsDto)
        {
            var eventEntity = await _eventsDbSet
                              .Include(x => x.EventOptions)
                              .Include(x => x.EventParticipants)
                              .Where(x => x.Id == changeOptionsDto.EventId && x.OrganizationId == changeOptionsDto.OrganizationId)
                              .Select(MapEventToJoinValidationDto)
                              .FirstOrDefaultAsync();

            _eventValidationService.CheckIfEventExists(eventEntity);

            // ReSharper disable once PossibleNullReferenceException
            eventEntity.SelectedOptions = eventEntity.Options
                                          .Where(option => changeOptionsDto.ChosenOptions.Contains(option.Id))
                                          .ToList();

            _eventValidationService.CheckIfRegistrationDeadlineIsExpired(eventEntity.RegistrationDeadline);
            _eventValidationService.CheckIfProvidedOptionsAreValid(changeOptionsDto.ChosenOptions, eventEntity.SelectedOptions);
            _eventValidationService.CheckIfJoiningNotEnoughChoicesProvided(eventEntity.MaxChoices, changeOptionsDto.ChosenOptions.Count());
            _eventValidationService.CheckIfJoiningTooManyChoicesProvided(eventEntity.MaxChoices, changeOptionsDto.ChosenOptions.Count());
            _eventValidationService.CheckIfSingleChoiceSelectedWithRule(eventEntity.SelectedOptions, OptionRules.IgnoreSingleJoin);
            _eventValidationService.CheckIfUserParticipatesInEvent(changeOptionsDto.UserId, eventEntity.Participants);

            await ValidateSingleJoinAsync(eventEntity, changeOptionsDto.OrganizationId, changeOptionsDto.UserId);

            var participant = await _eventParticipantsDbSet
                              .Include(x => x.EventOptions)
                              .FirstAsync(p => p.EventId == changeOptionsDto.EventId && p.ApplicationUserId == changeOptionsDto.UserId);

            participant.EventOptions.Clear();
            participant.EventOptions = eventEntity.SelectedOptions;

            await _uow.SaveChangesAsync(changeOptionsDto.UserId);
        }