| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | |
| | | 4 | | namespace SVETA.Api.Data.DTO |
| | | 5 | | { |
| | | 6 | | public class PaginationMetaDTO |
| | | 7 | | { |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// общее количество записей в таблице без учёта фильтрации |
| | | 11 | | /// </summary> |
| | 0 | 12 | | public int TotalCount { get; set; } |
| | | 13 | | /// <summary> |
| | | 14 | | /// общее количество записей с учётом фильтрации |
| | | 15 | | /// </summary> |
| | 0 | 16 | | public int TotalFilteredCount { get; set; } |
| | | 17 | | /// <summary> |
| | | 18 | | /// количество записей на страницу |
| | | 19 | | /// </summary> |
| | 0 | 20 | | public int Limit { get; set; } |
| | | 21 | | /// <summary> |
| | | 22 | | /// номер страницы |
| | | 23 | | /// </summary> |
| | 0 | 24 | | public int Page { get; set; } |
| | | 25 | | /// <summary> |
| | | 26 | | /// номер последней страницы \ общее число страниц |
| | | 27 | | /// </summary> |
| | 0 | 28 | | public int LastPage { get; set; } |
| | | 29 | | /// <summary> |
| | | 30 | | /// ссылка на следующую страницу (null, если мы на последней странице) |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public string NextPageUrl { get; set; } |
| | | 33 | | /// <summary> |
| | | 34 | | /// ссылка на предыдущую страницу (null, если мы на первой странице) |
| | | 35 | | /// </summary> |
| | 0 | 36 | | public string PrevPageUrl { get; set; } |
| | | 37 | | /// <summary> |
| | | 38 | | /// номер первой записи на странице |
| | | 39 | | /// </summary> |
| | 0 | 40 | | public int From { get; set; } |
| | | 41 | | /// <summary> |
| | | 42 | | /// номер последней записи на странице |
| | | 43 | | /// </summary> |
| | 0 | 44 | | public int To { get; set; } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public class BaseResponseDTO<T> |
| | | 48 | | { |
| | | 49 | | /// <summary> |
| | | 50 | | /// Базовый Response DTO |
| | | 51 | | /// </summary> |
| | | 52 | | /// <remarks>author: IPod</remarks> |
| | | 53 | | /// <param name="url">Ссылка на группу методов</param> |
| | | 54 | | /// <param name="page">Номер страницы</param> |
| | | 55 | | /// <param name="limit">Лимит записей на страницу</param> |
| | | 56 | | /// <param name="totalFilterCount">общее количество записей с учётом фильтрации</param> |
| | | 57 | | /// <param name="totalCount">общее количество записей в таблице без учёта фильтрации</param> |
| | | 58 | | /// <param name="sort">Параметр сортировки</param> |
| | | 59 | | /// <param name="filter">Фильтр для таблицы</param> |
| | | 60 | | /// <param name="param">Параметры запроса (Обычно идут в место фильтра)</param> |
| | | 61 | | public BaseResponseDTO(string url, int page, int limit, int totalFilterCount, int totalCount, string sort = "", |
| | | 62 | | { |
| | | 63 | | limit = limit > totalFilterCount ? totalFilterCount : limit; |
| | | 64 | | int remainder = 0; |
| | | 65 | | var quotient = totalFilterCount != 0 ? Math.DivRem(totalFilterCount, limit, out remainder) : 0; //получаем о |
| | | 66 | | |
| | | 67 | | Pagination = new PaginationMetaDTO(); |
| | | 68 | | |
| | | 69 | | Pagination.LastPage = remainder == 0 ? quotient : quotient + 1; |
| | | 70 | | page = page > Pagination.LastPage ? Pagination.LastPage : page; |
| | | 71 | | Pagination.From = totalFilterCount != 0 ? ((page - 1) * limit) + 1 : 0; |
| | | 72 | | Pagination.To = page >= Pagination.LastPage ? totalFilterCount : page * limit; |
| | | 73 | | Pagination.NextPageUrl = page >= Pagination.LastPage ? null : getUrl(url, sort, filter, page+1, limit, param |
| | | 74 | | Pagination.PrevPageUrl = (page - 1) <= 0 ? null : getUrl(url, sort, filter, page-1, limit, param); |
| | | 75 | | Pagination.Page = page; |
| | | 76 | | Pagination.Limit = limit; |
| | | 77 | | Pagination.TotalCount = totalCount; |
| | | 78 | | Pagination.TotalFilteredCount = totalFilterCount; |
| | | 79 | | } |
| | | 80 | | string getUrl(string url, string sort, string filter, int page, int limit, string param) |
| | | 81 | | { |
| | | 82 | | string result; |
| | | 83 | | if (!string.IsNullOrEmpty(filter)) |
| | | 84 | | result = $"{url}?page={page}&limit={limit}&filter={filter}"; |
| | | 85 | | else if (!string.IsNullOrEmpty(param)) |
| | | 86 | | result = $"{url}?{param}&page={page}&limit={limit}"; |
| | | 87 | | else |
| | | 88 | | result = $"{url}?page={page}&limit={limit}"; |
| | | 89 | | if (!string.IsNullOrEmpty(sort)) |
| | | 90 | | result += $"&sort={sort.Replace("|", "%7C")}"; |
| | | 91 | | return result; |
| | | 92 | | } |
| | | 93 | | /// <summary> |
| | | 94 | | /// Список возвращаемых объектов |
| | | 95 | | /// </summary> |
| | | 96 | | public List<T> Data { get; set; } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Метаданные пагинации |
| | | 100 | | /// </summary> |
| | | 101 | | public PaginationMetaDTO Pagination { get; set; } |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | public class BaseItemResponseDto<T>: BaseResponseDTO<T> |
| | | 105 | | { |
| | | 106 | | public BaseItemResponseDto(string url, int page, int limit, int totalFilterCount, int totalCount, string sort = |
| | | 107 | | : base(url, page, limit, totalFilterCount, totalCount, sort, filter, param) {} |
| | | 108 | | public T Data { get; set; } |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | public class BasePaginationResponseDto: PaginationMetaDTO |
| | | 112 | | { |
| | | 113 | | public BasePaginationResponseDto(string url, int page, int limit, int totalFilterCount, int totalCount, |
| | | 114 | | string sort = "", string filter = "", string param = "") |
| | | 115 | | { |
| | | 116 | | var data = new BaseResponseDTO<string>(url, page, limit, totalFilterCount, totalCount, sort, filter, param); |
| | | 117 | | this.From = data.Pagination.From; |
| | | 118 | | this.To = data.Pagination.To; |
| | | 119 | | this.NextPageUrl = data.Pagination.NextPageUrl; |
| | | 120 | | this.PrevPageUrl = data.Pagination.PrevPageUrl; |
| | | 121 | | this.Page = data.Pagination.Page; |
| | | 122 | | this.Limit = data.Pagination.Limit; |
| | | 123 | | this.TotalCount = data.Pagination.TotalCount; |
| | | 124 | | this.TotalFilteredCount = data.Pagination.TotalFilteredCount; |
| | | 125 | | } |
| | | 126 | | } |
| | | 127 | | } |