예제 #1
0
        public async Task <IActionResult> Index(IndexViewModel submittedSolution, [FromQuery] bool kiosk)
        {
            string submittedText = null;

            if (submittedSolution.Solution != null)
            {
                try
                {
                    using (var sr = new StreamReader(submittedSolution.Solution.OpenReadStream()))
                    {
                        submittedText = await sr.ReadToEndAsync();

                        var solution = submittedText.Split('\n').Select(l => l.Trim()).Where(l => l != "").Select(l => int.Parse(l)).Distinct().ToList();

                        if (solution.Count != ProblemGenerator.StepCount)
                        {
                            throw new Exception();
                        }

                        submittedSolution.Length = _solutionEvaluator.Evaluate(solution);

                        _mailer.SolutionAccepted(submittedSolution.EMail, submittedText);

                        await _solutionRepository.InsertSolution(new Repositories.Solution
                        {
                            Created    = DateTime.Now,
                            EMail      = submittedSolution.EMail,
                            Name       = submittedSolution.Name,
                            Length     = Convert.ToDecimal(submittedSolution.Length.Value),
                            Submission = submittedText
                        });
                    }
                }
                catch (Exception ex)
                {
                    submittedSolution.ProblemWithSolution = true;
                    throw;
                }
            }

            if (kiosk && !string.IsNullOrEmpty(submittedSolution.Name) && !string.IsNullOrEmpty(submittedSolution.EMail))
            {
                bool atLeastOneSucces = false;

                try
                {
                    await _registrationRepository.InsertRegistration(new Registration
                    {
                        EMail     = submittedSolution.EMail,
                        Name      = submittedSolution.Name,
                        OpenDoors = submittedSolution.OpenDoor
                    });

                    atLeastOneSucces = true;
                }
                catch (Exception ex)
                {
                }

                try
                {
                    var receiptResult = await _mailer.SendRegistrationReceiptToUs(submittedSolution.Name, submittedSolution.EMail, submittedSolution.OpenDoor);

                    var welcomeResult = await _mailer.SendWelcomeMailToThem(submittedSolution.EMail);

                    atLeastOneSucces = atLeastOneSucces || receiptResult || welcomeResult;
                }
                catch (Exception ex)
                {
                }


                if (atLeastOneSucces && kiosk)
                {
                    return(Redirect("/Home/RegistrationSuccess"));
                }
                else if (kiosk)
                {
                    submittedSolution.ProblemWithRegistration = true;
                }
            }

            if (!kiosk)
            {
                try
                {
                    submittedSolution.BestSolutions = await _solutionRepository.GetBestSolutions();
                }
                catch (Exception ex)
                {
                }
            }

            submittedSolution.Kiosk = kiosk;

            return(View(submittedSolution));
        }