Skip to content

Commit

Permalink
feat: random entry (#113)
Browse files Browse the repository at this point in the history
* feat: random entry

* docs+feat: api random route

* style: random is now inside the search

* docs: random query is now documented

* fix: page current now returns an integer number

* Update API.md

---------

Co-authored-by: Antonio Vivace <avivace4@gmail.com>
  • Loading branch information
dag7dev and avivace authored May 22, 2024
1 parent b076764 commit aae5aca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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),
# number of elements in this page
"page_elements": len(serializer.data),
# array of entries manifests
Expand Down

0 comments on commit aae5aca

Please sign in to comment.