Skip to content

Commit aae5aca

Browse files
dag7devavivace
andauthored
feat: random entry (#113)
* 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 <[email protected]>
1 parent b076764 commit aae5aca

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

API.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ Returns every entry in the database. Every entry is represented by its game mani
7676
Return every entry in the database matching the given conditions. Every entry is represented by its game manifest.
7777

7878
The following query parameters can be used:
79-
8079
- `type` (exact matching)
8180
- `developer` (exact matching)
8281
- `platform` (exact matching)
8382
- `tags` (exact matching, comma-separated array e.g. `/search?tags=Open Source,RPG`)
8483
- `title` ("contains" matching, e.g. `/search?title=brick` will return "**Brick**ster" and "**Brick**Breaker")
84+
- `random` (if true, the results will be in a random order)
8585

8686
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.
8787

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

97+
```bash
98+
# Get Game Boy Color games in a random order
99+
curl hh3.gbdev.io/api/search?random=true&platform=GBC
100+
```
101+
97102
### Pagination
98103

99104
Every result is paginated. These additional query params can be used in any of the previous routes:

hhub/views.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def entries_all(request):
7777
)
7878
return JsonResponse(serializer.data, safe=False)
7979

80-
8180
def search_entries(request):
8281
"""
8382
Returns every entry matching the conditions given in the query
@@ -93,6 +92,7 @@ def search_entries(request):
9392
tags = request.GET.get("tags", "")
9493
platform = request.GET.get("platform", "")
9594
text_query = request.GET.get("q", "")
95+
random_query = request.GET.get("random", False)
9696

9797
# Pagination
9898
# Request a specific page
@@ -139,6 +139,9 @@ def search_entries(request):
139139
)
140140
results = len(entries)
141141

142+
if random_query:
143+
entries = entries.order_by("?")
144+
142145
# Prepare paginators and number of results
143146
paginator = Paginator(entries, num_elements)
144147

@@ -171,7 +174,7 @@ def search_entries(request):
171174
# total number of pages
172175
"page_total": paginator.num_pages,
173176
# current request page
174-
"page_current": page,
177+
"page_current": int(page),
175178
# number of elements in this page
176179
"page_elements": len(serializer.data),
177180
# array of entries manifests

0 commit comments

Comments
 (0)