Ejemplo n.º 1
0
        /// <summary>
        /// The main extension method that takes a string and returns a football object.
        /// </summary>
        /// <param name="item"> The string we are processing. </param>
        /// <returns> The football validator type that contains a football object if validation passed. </returns>
        public static FootballValidatorType ToFootball(this string item)
        {
            var isFootballValidType = new FootballValidatorType();

            var(canExtract, errorMessage, data) = CanExtractFootballItems(item);
            if (canExtract)
            {
                isFootballValidType.ExtractFootballItems(data);
            }
            else
            {
                isFootballValidType.IsValid = false;
                isFootballValidType.ErrorList.Add(errorMessage);
            }

            return(isFootballValidType);
        }
Ejemplo n.º 2
0
        private static void ProcessFootballValidation(this FootballValidatorType isFootballValidType, Football result)
        {
            var validationResult = result.IsValid(new FootballValidator());

            if (validationResult.IsValid)
            {
                isFootballValidType.IsValid  = true;
                isFootballValidType.Football = result;
            }
            else
            {
                isFootballValidType.IsValid = false;
                isFootballValidType.ErrorList.Add($"Invalid Football.");

                foreach (var validationResultError in validationResult.Errors)
                {
                    isFootballValidType.ErrorList.Add(validationResultError.ErrorMessage);
                }
            }
        }
Ejemplo n.º 3
0
        private static void ExtractFootballItems(this FootballValidatorType isFootballValidType, IReadOnlyList <string> data)
        {
            var team          = data[0];
            var forPoints     = data[1];
            var againstPoints = data[2];

            if (int.TryParse(forPoints, out var forAsInt) && int.TryParse(againstPoints, out var againstAsInt))
            {
                var result = new Football
                {
                    TeamName      = team.Trim(),
                    ForPoints     = forAsInt,
                    AgainstPoints = againstAsInt
                };

                isFootballValidType.ProcessFootballValidation(result);
            }
            else
            {
                isFootballValidType.IsValid = false;
                isFootballValidType.ErrorList.Add($"Invalid items. Team: {team}, For Points: {forPoints}, Against Points: {againstPoints}.");
            }
        }