示例#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));
        }
        public async Task <IActionResult> Index(IndexViewModel submittedSolution, [FromQuery] bool kiosk)
        {
            if (!string.IsNullOrEmpty(submittedSolution.EMail))
            {
                _logger.LogInformation($"Seeing e-mail: {submittedSolution.EMail}");
            }

            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();

                        _logger.LogInformation($"Solution from {submittedSolution.EMail} to be evaluated");

                        submittedSolution.TotalCost = _solutionEvaluator.Evaluate(solution);

                        _logger.LogInformation($"Solution evaluated to {submittedSolution.TotalCost}");

                        _mailer.SolutionAccepted(submittedSolution.EMail, submittedText);
                    }
                }
                catch (Exception ex)
                {
                    submittedSolution.ProblemWithSolution = true;
                }
            }

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

                try
                {
                    var receiptResult = await _mailer.SendRegistrationReceiptToUs(submittedSolution);

                    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;
                }
            }

            submittedSolution.Kiosk = kiosk;

            return(View(submittedSolution));
        }