Added limit parameter to VideoAPI search and improved documentation

This commit is contained in:
david 2023-05-19 15:31:05 +02:00
parent bd28751013
commit c4af1d1808
2 changed files with 31 additions and 7 deletions

View File

@ -1,7 +1,26 @@
# API
## VideoAPI
# VideoAPI
## Search
`GET \api?param=value`
### Parameters
| Param | Value |
|-|-|
| `q` | The search query string. |
| `limit` | A limit on the number of objects to be returned. Default is 6. |
### Response
```
[
{
"id": 1,
"name": "Video 1"
},
{...}
]
```
### Examples
> `GET /api?q=foo&limit=3` will return the first 3 videos with "foo" in their name.
| Method | URL | Action |
|-|-|-|
| `GET`| `/api` | Get all videos |
| `GET`| `/api?q=<query>` | Get all videos containing `<query>` |

View File

@ -7,6 +7,11 @@ from core.serializers import VideoSerializer
@api_view(['GET'])
def videoAPI(request):
q = request.GET.get('q', '')
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))
l = request.GET.get('limit', '')
try:
l = int(l)
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))[:l]
except:
videos = Video.objects.filter(Q(id__contains=q) | Q(name__contains=q))
serializer = VideoSerializer(videos, many=True)
return Response(serializer.data)