public async Task <IActionResult> RegisterContestParticipant(RegisterContestParticipantViewModel viewModel)
        {
            try
            {
                await _handlerDispatcher.ExecuteCommandAsync(new RegisterContestParticipantCommand
                {
                    RegisterContestParticipantViewModel = viewModel,
                    Scheme     = Request.Scheme,
                    Controller = "Account",
                    Action     = nameof(AccountController.ConfirmEmail)
                });
            }
            catch (Exception ex) when(ex is ValidationException || ex is UnableToCreateUserException)
            {
                if (ex is ValidationException validException)
                {
                    validException.ValidationResult.ForEach(res => ModelState.AddModelError(res.Key, res.Value));
                }
                if (ex is UnableToCreateUserException unableException)
                {
                    ModelState.AddErrors(unableException.Errors);
                }

                await FillRegisterContestParticipantViewData();

                return(View(viewModel));
            }

            var contestType = await _handlerDispatcher.ExecuteQueryAsync(new GetContestTypeForHomeQuery { Id = viewModel.ContestId });

            return(contestType == ContestType.Individual ?
                   RedirectToAction(nameof(IndividualContestController.Register), "IndividualContest", new { id = viewModel.ContestId }) :
                   RedirectToAction(nameof(TeamContestController.Register), "TeamContest", new { id = viewModel.ContestId }));
        }
        public async Task <IActionResult> DeleteRegistration(int id)
        {
            //TODO На UI переспросить "Вы точно уверены, что хотите удалить регистрацию?"

            var command = new DeleteRegistrationCommand {
                RegistrationId = id
            };
            await HandlerDispatcher.ExecuteCommandAsync(command);

            return(RedirectToAction(nameof(Details), new { id = command.ContestId }));
        }
        public async Task <IActionResult> Index(IndexViewModel viewModel)
        {
            try
            {
                await _handlerDispatcher.ExecuteCommandAsync(new SaveUserCommand { ViewModel = viewModel });
            }
            catch (ValidationException e)
            {
                e.ValidationResult.ForEach(res => ModelState.AddModelError(res.Key, res.Value));
                await FillViewDataAsync(viewModel);

                return(View(viewModel));
            }

            StatusMessage = "Профиль был успешно обновлен.";
            return(RedirectToAction(nameof(Index)));
        }
Exemple #4
0
        public async Task <IActionResult> Login(LoginViewModel viewModel, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (!ModelState.IsValid)
            {
                // If we got this far, something failed, redisplay form
                return(View(viewModel));
            }

            try
            {
                await _handlerDispatcher.ExecuteCommandAsync(new LoginCommand { ViewModel = viewModel });
            }
            catch (Exception ex) when(ex is EntityNotFoundException || ex is EmailNotConfirmedException)
            {
                ModelState.AddModelError(string.Empty, "Пользователь не найден или email не подтвержден.");
                return(View(viewModel));
            }
            catch (UserLockedOutException)
            {
                return(RedirectToAction(nameof(Lockout)));
            }
            catch (InvalidLoginAttemptException)
            {
                ModelState.AddModelError(string.Empty, "Invalid login attempt.");
                return(View(viewModel));
            }

            return(RedirectToLocal(returnUrl));
        }
        public async Task <IActionResult> Create(TCreateViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                await FillViewDataForCreateAsync(viewModel);

                return(View(viewModel));
            }

            try
            {
                //TODO можно заменить фабричным методом
                var command = new TCreateEntityCommand {
                    Entity = viewModel
                };
                InitCreateCommand(command);

                await HandlerDispatcher.ExecuteCommandAsync(command);
            }
            catch (ValidationException e)
            {
                e.ValidationResult.ForEach(res => ModelState.AddModelError(res.Key, res.Value));
                await FillViewDataForCreateAsync(viewModel);

                return(View(viewModel));
            }
            //TODO можно обобщить исключение
            catch (UnableToCreateUserException e)
            {
                ModelState.AddErrors(e.Errors);
                await FillViewDataForCreateAsync(viewModel);

                return(View(viewModel));
            }

            return(RedirectToAction(nameof(Index)));
        }