| | 1 | | using Fluorite.Strainer.Exceptions; |
| | 2 | | using Fluorite.Strainer.Models.Metadata; |
| | 3 | |
|
| | 4 | | namespace Fluorite.Strainer.Services.Validation; |
| | 5 | |
|
| | 6 | | public class SortExpressionValidator : ISortExpressionValidator |
| | 7 | | { |
| | 8 | | public void Validate(IReadOnlyDictionary<Type, IReadOnlyDictionary<string, IPropertyMetadata>> propertyMetadata) |
| | 9 | | { |
| 11 | 10 | | Guard.Against.Null(propertyMetadata); |
| | 11 | |
|
| 22 | 12 | | foreach (var typeKeyPair in propertyMetadata) |
| | 13 | | { |
| 2 | 14 | | var type = typeKeyPair.Key; |
| 2 | 15 | | var properties = typeKeyPair.Value.Values; |
| 5 | 16 | | var defaultSortingProperties = properties.Where(x => x.IsDefaultSorting).ToList(); |
| | 17 | |
|
| 2 | 18 | | if (!defaultSortingProperties.Any()) |
| | 19 | | { |
| 1 | 20 | | var exceptionMessage = |
| 1 | 21 | | $"No default sort expression found for type {type.FullName}.\n" + |
| 1 | 22 | | $"Mark a property as default sorting to enable fallbacking " + |
| 1 | 23 | | $"to it when no sorting information is provided."; |
| | 24 | |
|
| 1 | 25 | | throw new StrainerSortExpressionValidatorException( |
| 1 | 26 | | type, |
| 1 | 27 | | exceptionMessage); |
| | 28 | | } |
| | 29 | |
|
| 1 | 30 | | if (defaultSortingProperties.Count > 1) |
| | 31 | | { |
| 1 | 32 | | var defaultProperties = string.Join( |
| 1 | 33 | | Environment.NewLine, |
| 3 | 34 | | defaultSortingProperties.Select(x => x.Name)); |
| 1 | 35 | | var exceptionMessage = |
| 1 | 36 | | $"Too many default sort properties found for type {type.FullName}.\n" + |
| 1 | 37 | | $"Only one property can be marked as default.\n" + |
| 1 | 38 | | $"Default properties:\n" + |
| 1 | 39 | | $"{defaultProperties}"; |
| | 40 | |
|
| 1 | 41 | | throw new StrainerSortExpressionValidatorException( |
| 1 | 42 | | type, |
| 1 | 43 | | exceptionMessage); |
| | 44 | | } |
| | 45 | | } |
| 8 | 46 | | } |
| | 47 | | } |