from django.db.models import Q
from rest_framework.response import Response
from rest_framework.decorators import api_view
from core.models import Video
from core.serializers import VideoSerializer

@api_view(['GET'])
def searchAPI(request):
    q = request.GET.get('q', '')
    l = request.GET.get('limit', '6')
    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)
