Skip to content

Commit

Permalink
Add sortOrder query param for Comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ioslife committed Jan 30, 2025
1 parent efcbc10 commit 81a3b27
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions public/API/API_GetComments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* t : 1 = game, 2 = achievement, 3 = user
* o : offset - number of entries to skip (default: 0)
* c : count - number of entries to return (default: 100, max: 500)
* s : sortOrder - sort comments. 0 = ascending, 1 = descending (default: 0)
*
* int Count number of comment records returned in the response
* int Total number of comment records the game/achievement/user actually has overall
Expand Down Expand Up @@ -43,12 +44,14 @@
],
'o' => ['sometimes', 'integer', 'min:0', 'nullable'],
'c' => ['sometimes', 'integer', 'min:1', 'max:500', 'nullable'],
's' => ['sometimes', 'integer', 'min:0', 'max:1', 'nullable'],
];

$input = Validator::validate(Arr::wrap($query), $rules);

$offset = $input['o'] ?? 0;
$count = $input['c'] ?? 100;
$sortOrder = $input['s'] ?? 0;

$username = null;
$gameOrAchievementId = 0;
Expand All @@ -75,7 +78,7 @@

$articleId = $user ? $user->ID : $gameOrAchievementId;

$comments = Comment::withTrashed()
$commentsQuery = Comment::withTrashed()
->with('user')
->where('ArticleType', $commentType)
->where('ArticleID', $articleId)
Expand All @@ -84,8 +87,13 @@
$query->whereNull('banned_at');
})
->offset($offset)
->limit($count)
->get();
->limit($count);

if ($sortOrder == 1) {
$commentsQuery->orderBy('Submitted', 'DESC');
}

$comments = $commentsQuery->get();

$totalComments = Comment::withTrashed()
->where('ArticleType', $commentType)
Expand Down

0 comments on commit 81a3b27

Please sign in to comment.