diff --git a/app/Models/BeatmapDiscussion.php b/app/Models/BeatmapDiscussion.php index 0e3b95e9077..06fb22bd261 100644 --- a/app/Models/BeatmapDiscussion.php +++ b/app/Models/BeatmapDiscussion.php @@ -71,14 +71,8 @@ class BeatmapDiscussion extends Model // FIXME: This and other static search functions should be extracted out. public static function search($rawParams = []) { - $pagination = pagination(cursor_from_params($rawParams) ?? $rawParams); + [$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams); - $params = [ - 'limit' => $pagination['limit'], - 'page' => $pagination['page'], - ]; - - $query = static::limit($params['limit'])->offset($pagination['offset']); $isModerator = $rawParams['is_moderator'] ?? false; if (present($rawParams['user'] ?? null)) { diff --git a/app/Models/BeatmapDiscussionPost.php b/app/Models/BeatmapDiscussionPost.php index 22d8efea995..a02f502f0e6 100644 --- a/app/Models/BeatmapDiscussionPost.php +++ b/app/Models/BeatmapDiscussionPost.php @@ -41,14 +41,8 @@ class BeatmapDiscussionPost extends Model implements Traits\ReportableInterface public static function search($rawParams = []) { - $pagination = pagination(cursor_from_params($rawParams) ?? $rawParams); + [$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams); - $params = [ - 'limit' => $pagination['limit'], - 'page' => $pagination['page'], - ]; - - $query = static::limit($params['limit'])->offset($pagination['offset']); $isModerator = $rawParams['is_moderator'] ?? false; if (isset($rawParams['user'])) { diff --git a/app/Models/BeatmapDiscussionVote.php b/app/Models/BeatmapDiscussionVote.php index 0b8dd6fc8ba..7006417adcc 100644 --- a/app/Models/BeatmapDiscussionVote.php +++ b/app/Models/BeatmapDiscussionVote.php @@ -57,14 +57,8 @@ public static function recentlyGivenByUser($userId, $timeframeMonths = 3) public static function search($rawParams = []) { - $pagination = pagination(cursor_from_params($rawParams) ?? $rawParams); + [$query, $params] = static::searchQueryAndParams(cursor_from_params($rawParams) ?? $rawParams); - $params = [ - 'limit' => $pagination['limit'], - 'page' => $pagination['page'], - ]; - - $query = static::limit($params['limit'])->offset($pagination['offset']); $isModerator = $rawParams['is_moderator'] ?? false; if (isset($rawParams['user'])) { diff --git a/app/Models/BeatmapsetEvent.php b/app/Models/BeatmapsetEvent.php index d5331a8d1d8..2d0fabf891d 100644 --- a/app/Models/BeatmapsetEvent.php +++ b/app/Models/BeatmapsetEvent.php @@ -83,14 +83,8 @@ public static function log($type, $user, $object, $extraData = []) public static function search($rawParams = []) { - $pagination = pagination($rawParams); + [$query, $params] = static::searchQueryAndParams($rawParams); - $params = [ - 'limit' => $pagination['limit'], - 'page' => $pagination['page'], - ]; - - $query = static::limit($params['limit'])->offset($pagination['offset']); $searchByUser = present($rawParams['user'] ?? null); $isModerator = $rawParams['is_moderator'] ?? false; diff --git a/app/Models/Model.php b/app/Models/Model.php index 49c27893450..df5caf449b0 100644 --- a/app/Models/Model.php +++ b/app/Models/Model.php @@ -35,6 +35,19 @@ public static function booted() static::addGlobalScope(new MacroableModelScope()); } + protected static function searchQueryAndParams(array $params) + { + $limit = clamp(get_int($params['limit'] ?? null) ?? static::PER_PAGE, 5, 50); + $page = max(get_int($params['page'] ?? null), 1); + + $offset = max_offset($page, $limit); + $page = 1 + $offset / $limit; + + $query = static::limit($limit)->offset($offset); + + return [$query, compact('limit', 'page')]; + } + public function getForeignKey() { if ($this->primaryKey === null || $this->primaryKey === 'id') { diff --git a/app/helpers.php b/app/helpers.php index 23dc6dd75c5..51053a3aec2 100644 --- a/app/helpers.php +++ b/app/helpers.php @@ -651,17 +651,6 @@ function pack_str($str) return pack('ccH*', 0x0b, strlen($str), bin2hex($str)); } -function pagination($params, $defaults = null) -{ - $limit = clamp(get_int($params['limit'] ?? null) ?? $defaults['limit'] ?? 20, 5, 50); - $page = max(get_int($params['page'] ?? null) ?? 1, 1); - - $offset = max_offset($page, $limit); - $page = 1 + $offset / $limit; - - return compact('limit', 'page', 'offset'); -} - function product_quantity_options($product, $selected = null) { if ($product->stock === null) {