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

feat: random entry #113

Merged
merged 6 commits into from
May 22, 2024
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
7 changes: 6 additions & 1 deletion API.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ Returns every entry in the database. Every entry is represented by its game mani
Return every entry in the database matching the given conditions. Every entry is represented by its game manifest.

The following query parameters can be used:

- `type` (exact matching)
- `developer` (exact matching)
- `platform` (exact matching)
- `tags` (exact matching, comma-separated array e.g. `/search?tags=Open Source,RPG`)
- `title` ("contains" matching, e.g. `/search?title=brick` will return "**Brick**ster" and "**Brick**Breaker")
- `random` (if true, the results will be in a random order)

More than one query parameter can be specified. They will be concatenated in "AND" statements, i.e. `/search?type=homebrew&platform=GBC` will return every Homebrew developed with GBC features.

Expand All @@ -94,6 +94,11 @@ Every matching is case-insensitive.
curl hh3.gbdev.io/api/search?tags=Open Source,RPG&platform=GBC
```

```bash
# Get Game Boy Color games in a random order
curl hh3.gbdev.io/api/search?random=true&platform=GBC
```

### Pagination

Every result is paginated. These additional query params can be used in any of the previous routes:
Expand Down
7 changes: 5 additions & 2 deletions hhub/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ def entries_all(request):
)
return JsonResponse(serializer.data, safe=False)


def search_entries(request):
"""
Returns every entry matching the conditions given in the query
Expand All @@ -93,6 +92,7 @@ def search_entries(request):
tags = request.GET.get("tags", "")
platform = request.GET.get("platform", "")
text_query = request.GET.get("q", "")
random_query = request.GET.get("random", False)

# Pagination
# Request a specific page
Expand Down Expand Up @@ -139,6 +139,9 @@ def search_entries(request):
)
results = len(entries)

if random_query:
entries = entries.order_by("?")

# Prepare paginators and number of results
paginator = Paginator(entries, num_elements)

Expand Down Expand Up @@ -171,7 +174,7 @@ def search_entries(request):
# total number of pages
"page_total": paginator.num_pages,
# current request page
"page_current": page,
"page_current": int(page),
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are casting to int here? Pagination is already trying to the desired page here so if something is wrong (e.g. page is a string) it already fails there (and goes to page 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will return a string otherwise, and... this is not consistent with the normal backend behavior

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What? That's not true. At this line, page is used to paginate, if it's not a proper integer it would've failed there, so there's no way the code progress with page being a string.

# number of elements in this page
"page_elements": len(serializer.data),
# array of entries manifests
Expand Down
Loading