Пример #1
0
        public async Task CheckForDuplicateEntry()
        {
            var entry = new Step
            {
                Nickname    = "batman",
                JourneyName = "Test",
                TeamName    = "Test Team",
                Steps       = 100,
                Created     = DateTime.Now,
                DateStepped = DateTime.Now
            };

            var checkSubmitted = await entry.PostAsync(_azureSql);

            Assert.True(checkSubmitted);

            var checkDuplicate = await entry.CheckForDuplicateAsync(_azureSql);

            Assert.True(checkDuplicate);
        }
Пример #2
0
        public async Task <IActionResult> AddSteps([Bind("Nickname,JourneyName,TeamName,Steps,DateStepped,StepsTaken,StepsInRoute")] Step step)
        {
            if (step != null && !string.IsNullOrWhiteSpace(step.JourneyName) && (step.JourneyName.Length < 50) && !string.IsNullOrWhiteSpace(step.Nickname) && (step.Nickname.Length < 50) && step.Steps > 1)
            {
                step.Nickname    = step.Nickname.Trim();
                step.JourneyName = step.JourneyName.Trim();

                // Set the team name if one is not supplied.
                if (string.IsNullOrWhiteSpace(step.TeamName))
                {
                    step.TeamName = $"{step.JourneyName} Team";
                }

                // Block step values that are too large.
                if (step.StepsInRoute - (step.StepsTaken + step.Steps) < 0)
                {
                    // Let them know this was bad input maybe?
                    return(RedirectToAction("Index"));
                }

                // Prevent the submission of duplicate entries on the same day.
                var checkDuplicate = await step.CheckForDuplicateAsync(_azureSQL);

                if (checkDuplicate)
                {
                    var results = await Step.GetByNicknameAsync(step.Nickname, _azureSQL);

                    if (results.Any())
                    {
                        return(View("AddSteps", results));
                    }

                    results = await Step.GetByExactNicknameAsync(step.Nickname, _azureSQL);

                    return(View("AddSteps", results));
                }

                var teams = await Team.GetByTeamNameAsync(step.TeamName, _azureSQL);

                var team = teams.FirstOrDefault();

                if (team is null)
                {
                    var newTeam = new Team
                    {
                        TeamName    = step.TeamName,
                        Bio         = "Give me a cool bio.",
                        Active      = true,
                        Created     = DateTime.Now,
                        TeamWebsite = "https://thomasryan.xyz/",
                    };

                    var checkCreateTeam = await newTeam.PostAsync(_azureSQL);

                    if (!checkCreateTeam)
                    {
                        // Revert to the default if creating a new Team fails.
                        step.TeamName = $"{step.JourneyName} Team";
                    }
                }

                // Save this entry to the db.
                var checkSubmitted = await step.PostAsync(_azureSQL);

                if (checkSubmitted)
                {
                    var results = await Step.GetByNicknameAsync(step.Nickname, _azureSQL);

                    if (results.Any())
                    {
                        return(View("AddSteps", results));
                    }

                    results = await Step.GetByExactNicknameAsync(step.Nickname, _azureSQL);

                    return(View("AddSteps", results));
                }
            }
            // Submit steps anonymously.
            else if (step != null && !string.IsNullOrWhiteSpace(step.JourneyName) && (step.JourneyName.Length < 50) && string.IsNullOrWhiteSpace(step.Nickname) && step.Steps > 1)
            {
                step.Nickname    = $"{step.JourneyName} Team";
                step.JourneyName = step.JourneyName.Trim();

                // Set the team name if one is not supplied.
                if (string.IsNullOrWhiteSpace(step.TeamName))
                {
                    step.TeamName = $"{step.JourneyName} Team";
                }

                // Block step values that are too large.
                if (step.StepsInRoute - (step.StepsTaken + step.Steps) < 0)
                {
                    // Let them know this was bad input maybe?
                    return(Redirect($"/{step.JourneyName}/{step.TeamName}/"));
                }

                // Save this entry to the db.
                var checkSubmitted = await step.PostAsync(_azureSQL);

                if (checkSubmitted)
                {
                    var results = await Step.GetByNicknameAsync(step.Nickname, _azureSQL);

                    if (results.Any())
                    {
                        return(View("AddSteps", results));
                    }

                    results = await Step.GetByExactNicknameAsync(step.Nickname, _azureSQL);

                    return(View("AddSteps", results));
                }
            }
            // Show the page for the user.
            else if (step != null && step.Steps == 0 && !string.IsNullOrWhiteSpace(step.JourneyName) && (step.JourneyName.Length < 50) && !string.IsNullOrWhiteSpace(step.Nickname) && (step.Nickname.Length < 50))
            {
                step.Nickname    = step.Nickname.Trim();
                step.JourneyName = step.JourneyName.Trim();

                var results = await Step.GetByNicknameAsync(step.Nickname, _azureSQL);

                if (results.Any())
                {
                    return(View("AddSteps", results));
                }

                results = await Step.GetByExactNicknameAsync(step.Nickname, _azureSQL);

                return(View("AddSteps", results));
            }

            return(Redirect($"/{step?.JourneyName}"));
        }