Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup discussion related search query and params helper #11499

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions app/Models/BeatmapDiscussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
8 changes: 1 addition & 7 deletions app/Models/BeatmapDiscussionPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
8 changes: 1 addition & 7 deletions app/Models/BeatmapDiscussionVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand Down
8 changes: 1 addition & 7 deletions app/Models/BeatmapsetEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
13 changes: 13 additions & 0 deletions app/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down
11 changes: 0 additions & 11 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down