| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel; |
| | 4 | | using System.Globalization; |
| | 5 | | using System.Linq; |
| | 6 | | using System.Reflection; |
| | 7 | | using System.Text.RegularExpressions; |
| | 8 | | using Microsoft.AspNetCore.Http; |
| | 9 | |
|
| | 10 | | namespace ApplicationCore.Model |
| | 11 | | { |
| | 12 | | public class CommentForm : ICommentForm |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// Simplest form of email validation |
| | 16 | | /// </summary> |
| 1 | 17 | | private static readonly Regex _validEmail = new Regex(@"^[^@\s]+@[^@\s]+\.[^@\s]+$"); |
| | 18 | |
|
| | 19 | | private readonly Dictionary<string, FormField> _fields; |
| | 20 | | private readonly ConstructorInfo _constructor; |
| | 21 | |
|
| 26 | 22 | | public IEnumerable<string> Errors { get; } |
| | 23 | |
|
| 10 | 24 | | public bool HasErrors => Errors.Any(); |
| | 25 | |
|
| 15 | 26 | | public CommentForm(IFormCollection form) |
| 15 | 27 | | { |
| 15 | 28 | | _constructor = typeof(Comment).GetConstructors()[0]; |
| | 29 | |
|
| 15 | 30 | | _fields = _constructor.GetParameters().ToDictionary( |
| 120 | 31 | | p => p.Name!, |
| 120 | 32 | | p => TryConvertFormFieldValue(form[p.Name], p)); |
| | 33 | |
|
| 15 | 34 | | var errors = _fields |
| 120 | 35 | | .Where(p => p.Value.HasError) |
| 30 | 36 | | .Select(p => p.Value.Error) |
| 15 | 37 | | .ToList(); |
| | 38 | |
|
| 15 | 39 | | if (!IsEmailValid()) |
| 1 | 40 | | { |
| 1 | 41 | | errors.Add("email is not in the correct format"); |
| 1 | 42 | | } |
| | 43 | |
|
| 15 | 44 | | Errors = errors; |
| 15 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Try to create a Comment from the form. Each Comment constructor argument will be name-matched |
| | 49 | | /// against values in the form. Each non-optional arguments (those that don't have a default value) |
| | 50 | | /// not supplied will cause an error in the list of errors and prevent the Comment from being created. |
| | 51 | | /// </summary> |
| | 52 | | public CommentResult TryCreateComment() |
| 2 | 53 | | { |
| 2 | 54 | | if (!Errors.Any()) |
| 1 | 55 | | { |
| 8 | 56 | | return new CommentResult((Comment)_constructor.Invoke(_fields.Values.Select(x => x.Value).ToArray())); |
| | 57 | | } |
| | 58 | | else |
| 1 | 59 | | { |
| 1 | 60 | | return new CommentResult(new Comment(string.Empty, string.Empty, string.Empty), Errors); |
| | 61 | | } |
| 2 | 62 | | } |
| | 63 | |
|
| | 64 | | private static FormField TryConvertFormFieldValue(string fieldValue, ParameterInfo parameterInfo) |
| 105 | 65 | | { |
| 105 | 66 | | if (IsRequiredField()) |
| 31 | 67 | | { |
| 31 | 68 | | return new FormField(TypeDescriptor.GetConverter(parameterInfo.ParameterType).ConvertFrom(fieldValue)); |
| | 69 | | } |
| 74 | 70 | | else if (IsOptionalField()) |
| 60 | 71 | | { |
| 60 | 72 | | if (!string.IsNullOrEmpty(fieldValue)) |
| 13 | 73 | | { |
| 13 | 74 | | var converter = TypeDescriptor.GetConverter(parameterInfo.ParameterType); |
| | 75 | |
|
| 13 | 76 | | if (converter.IsValid(fieldValue)) |
| 12 | 77 | | { |
| 12 | 78 | | var convertedValue = converter.ConvertFrom(fieldValue); |
| 12 | 79 | | return new FormField(convertedValue); |
| | 80 | | } |
| | 81 | | else |
| 1 | 82 | | { |
| 1 | 83 | | return new FormField(string.Format( |
| 1 | 84 | | CultureInfo.InvariantCulture, |
| 1 | 85 | | CommentResources.InvalidTypeConversionErrorMessage, |
| 1 | 86 | | fieldValue, |
| 1 | 87 | | parameterInfo.ParameterType.ToString())); |
| | 88 | | } |
| | 89 | | } |
| | 90 | | else |
| 47 | 91 | | { |
| 47 | 92 | | return new FormField(parameterInfo.DefaultValue); |
| | 93 | | } |
| | 94 | | } |
| | 95 | | else |
| 14 | 96 | | { |
| | 97 | | // Field is required but missing a value |
| 14 | 98 | | return new FormField(string.Format( |
| 14 | 99 | | CultureInfo.InvariantCulture, |
| 14 | 100 | | CommentResources.MissingRequiredValueErrorMessage, |
| 14 | 101 | | parameterInfo.Name)); |
| | 102 | | } |
| | 103 | |
|
| 105 | 104 | | bool IsRequiredField() => !string.IsNullOrWhiteSpace(fieldValue) && !parameterInfo.HasDefaultValue; |
| 74 | 105 | | bool IsOptionalField() => parameterInfo.HasDefaultValue; |
| 105 | 106 | | } |
| | 107 | |
|
| | 108 | | // TODO: Remove magic string "email" |
| | 109 | | private bool IsEmailValid() |
| 15 | 110 | | { |
| 15 | 111 | | var email = _fields["email"].Value as string; |
| | 112 | |
|
| 15 | 113 | | if (string.IsNullOrEmpty(email)) |
| 10 | 114 | | { |
| 10 | 115 | | return true; |
| | 116 | | } |
| | 117 | |
|
| 5 | 118 | | return _validEmail.IsMatch(email); |
| 15 | 119 | | } |
| | 120 | | } |
| | 121 | | } |