Pagination.cs 3.6 KB

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