-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
feat(api): Add sortOrder to API_GetComments #3156
Open
ioslife
wants to merge
4
commits into
RetroAchievements:master
Choose a base branch
from
ioslife:api/get-comments-descending
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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; | ||
|
@@ -75,7 +78,7 @@ | |
|
||
$articleId = $user ? $user->ID : $gameOrAchievementId; | ||
|
||
$comments = Comment::withTrashed() | ||
$commentsQuery = Comment::withTrashed() | ||
->with('user') | ||
->where('ArticleType', $commentType) | ||
->where('ArticleID', $articleId) | ||
|
@@ -84,8 +87,13 @@ | |
$query->whereNull('banned_at'); | ||
}) | ||
->offset($offset) | ||
->limit($count) | ||
->get(); | ||
->limit($count); | ||
|
||
if ($sortOrder == 1) { | ||
$commentsQuery->orderBy('Submitted', 'DESC'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You'll probably want an explicit |
||
|
||
$comments = $commentsQuery->get(); | ||
|
||
$totalComments = Comment::withTrashed() | ||
->where('ArticleType', $commentType) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be too ambiguous in terms of what the
orderBy()
is. I'm also thinking integers as possible values may be inflexible if this pattern is extended to other API endpoints.I think we may want to change this to use JSON:API-like enum values, ie:
createdAt
and-createdAt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do have a few public APIs where filter parameters are effectively booleans (0=all,1=filtered), and some that are direct mapping of enums (3=core,5=unofficial).
But as this is not an enum, and it's not being implemented as a boolean (
s: sort descending (0=no,1=yes)
), I agree that it makes sense to have it be more free-form.