| | 1 | | using Plainquire.Page.Abstractions; |
| | 2 | | using System; |
| | 3 | | using System.Globalization; |
| | 4 | |
|
| | 5 | | namespace Plainquire.Page; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Extension methods for <see cref="EntityPage"/> |
| | 9 | | /// </summary> |
| | 10 | | public static class EntityPageExtensions |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Get the skip and take values for the given <paramref name="page"/>. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="page"></param> |
| | 16 | | /// <exception cref="InvalidOperationException"></exception> |
| | 17 | | public static (int? Skip, int? Take) GetSkipAndTake(this EntityPage page) |
| | 18 | | { |
| 256 | 19 | | var configuration = page.Configuration ?? PageConfiguration.Default ?? new PageConfiguration(); |
| | 20 | |
|
| 256 | 21 | | var pageNumber = ParsePageNumber(page, configuration); |
| 232 | 22 | | var pageSize = ParsePageSize(page, configuration); |
| | 23 | |
|
| 201 | 24 | | var skip = (pageNumber - 1) * pageSize; |
| | 25 | |
|
| 201 | 26 | | return (skip, pageSize); |
| | 27 | | } |
| | 28 | |
|
| | 29 | | private static int? ParsePageNumber(EntityPage page, PageConfiguration configuration) |
| | 30 | | { |
| 256 | 31 | | var pageNumberNotSet = string.IsNullOrEmpty(page.PageNumberValue); |
| 256 | 32 | | if (pageNumberNotSet) |
| 60 | 33 | | return null; |
| | 34 | |
|
| 196 | 35 | | var parseFailed = !int.TryParse(page.PageNumberValue, NumberStyles.None, CultureInfo.InvariantCulture, out var p |
| 196 | 36 | | if (parseFailed && !configuration.IgnoreParseExceptions) |
| 18 | 37 | | throw new FormatException($"Error while parsing page number '{page.PageNumberValue}'"); |
| | 38 | |
|
| 178 | 39 | | if (pageNumber >= 1) |
| 154 | 40 | | return pageNumber; |
| | 41 | |
|
| 24 | 42 | | if (configuration.IgnoreParseExceptions) |
| 18 | 43 | | return 1; |
| | 44 | |
|
| 6 | 45 | | throw new FormatException($"{nameof(EntityPage.PageNumber)} must be a positive integer, found: {page.PageNumber} |
| | 46 | | } |
| | 47 | |
|
| | 48 | | private static int? ParsePageSize(EntityPage page, PageConfiguration configuration) |
| | 49 | | { |
| 232 | 50 | | var pageSizeNotSet = string.IsNullOrEmpty(page.PageSizeValue); |
| 232 | 51 | | if (pageSizeNotSet && !configuration.IgnoreParseExceptions) |
| 12 | 52 | | throw new InvalidOperationException("Page size not set"); |
| | 53 | |
|
| 220 | 54 | | var parseFailed = !int.TryParse(page.PageSizeValue, NumberStyles.None, CultureInfo.InvariantCulture, out var pag |
| 220 | 55 | | if (parseFailed && !configuration.IgnoreParseExceptions) |
| 13 | 56 | | throw new FormatException($"Error while parsing page size '{page.PageSizeValue}'"); |
| | 57 | |
|
| 207 | 58 | | if (pageSize >= 1) |
| 168 | 59 | | return pageSize; |
| | 60 | |
|
| 39 | 61 | | if (configuration.IgnoreParseExceptions) |
| 33 | 62 | | return int.MaxValue; |
| | 63 | |
|
| 6 | 64 | | throw new FormatException($"{nameof(EntityPage.PageSize)} must be a positive integer, found: {page.PageSize}."); |
| | 65 | | } |
| | 66 | | } |