using Microsoft.AspNetCore.WebUtilities; using System.Reflection; namespace bitforum.Helpers { public class Pagination { public int Page { get; set; } = 0; // 현재 페이지 public int TotalRows { get; set; } = 0; // 전체 항목 수 public int TotalPage { get; set; } = 0; // 전체 페이지 수 public int PerPage = 10; public int StartPage = 0; // 현재 페이지의 시작 번호 public int EndPage = 0; // 현재 페이지의 마지막 번호 public int PageGroupSize = 10; // 한번에 보여줄 페이지 번호 개수 public int CurrentPageGroup = 0; // 현재 페이지가 속한 페이지 그룹의 시작번호 public int GroupStartPage = 0; // 페이지 그룹의 시작 페이지 번호 public int GroupEndPage = 0; // 페이지 그룹의 마지막 페이지 번호 public int PrevGroupPage = 0; // 이전 페이지 그룹의 페이지 번호 public int NextGroupPage = 0; // 다음 페이지 그룹 페이지 번호 private Dictionary QueryStringParams; public Pagination(int? totalRows, int page, int perPage, object? queryParams) { Page = page; PerPage = perPage; TotalRows = totalRows ?? 0; TotalPage = (int)Math.Ceiling((double)TotalRows / perPage); CurrentPageGroup = (int)Math.Ceiling((double)Page / PageGroupSize); StartPage = (CurrentPageGroup - 1) * PageGroupSize + 1; EndPage = Math.Min(StartPage + PageGroupSize - 1, TotalPage); GroupStartPage = GroupStartPage - PageGroupSize; GroupEndPage = ((Page - 1) / PageGroupSize + 1) * PageGroupSize; PrevGroupPage = Math.Max(StartPage - PageGroupSize, 1); // 이전 페이지 그룹의 첫 페이지 계산 NextGroupPage = Math.Min(EndPage + 1, TotalPage); // 다음 페이지 그룹의 첫 페이지 계산 // 객체를 쿼리스트링으로 변환 QueryStringParams = ToDictionary(queryParams); } // 이전 페이지 여부 public bool HasPreviousPage => Page > 1; // 다음 페이지 여부 public bool HasNextPage => Page < TotalPage; // 객체를 Dictionary로 변환하는 메서드 private Dictionary ToDictionary(object? obj) { var dictionary = new Dictionary(); if (obj is null) { return dictionary; } var properties = obj.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance); foreach (var prop in properties) { var value = prop.GetValue(obj)?.ToString(); if (!string.IsNullOrEmpty(value)) { dictionary.Add(prop.Name, value); } } return dictionary; } // 최종 쿼리스트링을 반환하는 메서드 public string BuildQueryString() { if (QueryStringParams == null || QueryStringParams.Count == 0) { return ""; } // 쿼리스트링이 있을 경우, AddQueryString으로 변환 후 ? 제거 return "&" + QueryHelpers.AddQueryString("", QueryStringParams).TrimStart('?'); } } }