diff --git a/api/README.md b/api/README.md index 057fcf1..cb1174b 100644 --- a/api/README.md +++ b/api/README.md @@ -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=` | Get all videos containing `` | diff --git a/api/views.py b/api/views.py index 7474215..db931e7 100644 --- a/api/views.py +++ b/api/views.py @@ -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)