Pagination.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Microsoft.AspNetCore.WebUtilities;
  2. using System.Reflection;
  3. namespace SharedKernel.Helpers
  4. {
  5. public class Pagination
  6. {
  7. public int Page { get; set; } = 1; // 현재 페이지
  8. public int TotalRows { get; set; } = 0; // 전체 항목 수
  9. public int TotalPage { get; set; } = 0; // 전체 페이지 수
  10. public int PerPage = 10;
  11. public int StartPage = 0; // 현재 페이지의 시작 번호
  12. public int EndPage = 0; // 현재 페이지의 마지막 번호
  13. public int PageGroupSize = 10; // 한번에 보여줄 페이지 번호 개수
  14. public int CurrentPageGroup = 0; // 현재 페이지가 속한 페이지 그룹의 시작번호
  15. public int GroupStartPage = 0; // 페이지 그룹의 시작 페이지 번호
  16. public int GroupEndPage = 0; // 페이지 그룹의 마지막 페이지 번호
  17. public int PrevGroupPage = 0; // 이전 페이지 그룹의 페이지 번호
  18. public int NextGroupPage = 0; // 다음 페이지 그룹 페이지 번호
  19. private Dictionary<string, string?> QueryStringParams;
  20. public Pagination(long totalRows, int page, int perPage, object? queryParams) : this((int)totalRows, page, perPage, queryParams) { }
  21. public Pagination(int totalRows, int page, int perPage, object? queryParams = null)
  22. {
  23. Page = page;
  24. PerPage = perPage;
  25. TotalRows = Math.Max(totalRows, 0);
  26. TotalPage = (int)Math.Ceiling((double)TotalRows / perPage);
  27. CurrentPageGroup = (Page - 1) / PageGroupSize + 1;
  28. StartPage = (CurrentPageGroup - 1) * PageGroupSize + 1;
  29. EndPage = Math.Min(StartPage + PageGroupSize - 1, TotalPage);
  30. GroupStartPage = StartPage;
  31. GroupEndPage = EndPage;
  32. PrevGroupPage = Math.Max(StartPage - PageGroupSize, 1); // 이전 페이지 그룹의 첫 페이지 계산
  33. NextGroupPage = Math.Min(EndPage + 1, TotalPage); // 다음 페이지 그룹의 첫 페이지 계산
  34. // 객체를 쿼리스트링으로 변환
  35. QueryStringParams = ToDictionary(queryParams);
  36. }
  37. // 이전 페이지 여부
  38. public bool HasPreviousPage => Page > 1;
  39. // 다음 페이지 여부
  40. public bool HasNextPage => Page < TotalPage;
  41. // 객체를 Dictionary로 변환하는 메서드
  42. private static Dictionary<string, string?> ToDictionary(object? obj)
  43. {
  44. var dictionary = new Dictionary<string, string?>();
  45. if (obj is null)
  46. {
  47. return dictionary;
  48. }
  49. // 문자열이면 직접 분리
  50. if (obj is string str)
  51. {
  52. var parsed = QueryHelpers.ParseQuery(str);
  53. foreach (var row in parsed)
  54. {
  55. dictionary[row.Key] = row.Value.ToString();
  56. }
  57. return dictionary;
  58. }
  59. // 일반 객체는 reflection을 사용하여 속성들을 가져옴
  60. var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
  61. foreach (var prop in properties)
  62. {
  63. var value = prop.GetValue(obj)?.ToString();
  64. if (!string.IsNullOrEmpty(value))
  65. {
  66. dictionary[prop.Name] = value;
  67. }
  68. }
  69. return dictionary;
  70. }
  71. // 최종 쿼리스트링을 반환하는 메서드
  72. public string BuildQueryString()
  73. {
  74. if (QueryStringParams == null || QueryStringParams.Count == 0)
  75. {
  76. return "";
  77. }
  78. var queryString = QueryHelpers.AddQueryString("", QueryStringParams); // ?key=value&key2=value2
  79. if (string.IsNullOrEmpty(queryString))
  80. {
  81. return "";
  82. }
  83. return "&" + queryString.TrimStart('?');
  84. }
  85. }
  86. }