From 0cdc001b5068ca6aaddc0132849a3c6213374e50 Mon Sep 17 00:00:00 2001 From: mortezaei Date: Sat, 8 Nov 2025 14:58:32 +0330 Subject: [PATCH] feat(library): add endpoint for listing downloaded books and remove filtering from book list - Introduced a new `DownloadedBooksListView` to retrieve books downloaded by the current user. - Updated `urls.py` to include the new endpoint `/books/downloaded/`. - Removed the `is_downloaded` parameter from the `BookListView` to streamline the book listing process. --- apps/library/doc.py | 13 ++------- apps/library/urls.py | 2 ++ apps/library/views.py | 65 +++++++++++++++++++++++++++++++++++-------- 3 files changed, 58 insertions(+), 22 deletions(-) diff --git a/apps/library/doc.py b/apps/library/doc.py index 3f4b106..4995d99 100644 --- a/apps/library/doc.py +++ b/apps/library/doc.py @@ -41,14 +41,6 @@ is_bookmark_param = openapi.Parameter( required=False ) -is_downloaded_param = openapi.Parameter( - 'is_downloaded', - openapi.IN_QUERY, - description="Filter books that are downloaded by the current user (set to 'true' to enable)", - type=openapi.TYPE_BOOLEAN, - required=False -) - search_param = openapi.Parameter( 'search', openapi.IN_QUERY, @@ -189,13 +181,14 @@ book_list_swagger = swagger_auto_schema( - Middle section collection using the query parameter 'middle' - Bottom section collection using the query parameter 'bottom' - Bookmarked books using the query parameter 'is_bookmark=true' - - Downloaded books using the query parameter 'is_downloaded=true' You can also search for books by title, summary, or author using the query parameter 'search'. + + Note: To get downloaded books, use the separate endpoint /books/downloaded/ """, operation_summary="List Books", tags=["Library"], - manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_param, is_downloaded_param, search_param], + manual_parameters=[collection_id_param, middle_param, bottom_param, is_bookmark_param, search_param], responses={ 200: books_response, 401: "Authentication credentials were not provided or are invalid.", diff --git a/apps/library/urls.py b/apps/library/urls.py index ca96f53..b5099fc 100644 --- a/apps/library/urls.py +++ b/apps/library/urls.py @@ -6,6 +6,7 @@ from apps.library.views import ( MiddleBookCollectionListView, BookListView, BookDetailView, + DownloadedBooksListView, BookDownloadCreateAPIView, ) @@ -15,5 +16,6 @@ urlpatterns = [ path('collections/', MiddleBookCollectionListView.as_view(), name='collection-list'), path('books/', BookListView.as_view(), name='book-list'), path('books//', BookDetailView.as_view(), name='book-detail'), + path('books/downloaded/', DownloadedBooksListView.as_view(), name='downloaded-books-list'), path('books/download/', BookDownloadCreateAPIView.as_view(), name='book-download'), ] \ No newline at end of file diff --git a/apps/library/views.py b/apps/library/views.py index 8cb62a8..c415bea 100644 --- a/apps/library/views.py +++ b/apps/library/views.py @@ -137,18 +137,6 @@ class BookListView(ListAPIView): # Filter books by these IDs queryset = queryset.filter(id__in=bookmarked_ids) - # Filter by downloaded books if requested - is_downloaded = self.request.query_params.get('is_downloaded', '').lower() - if is_downloaded == 'true': - # Get all downloaded book IDs for the current user - downloaded_ids = BookDownload.objects.filter( - user=self.request.user, - status=True - ).values_list('book_id', flat=True) - - # Filter books by these IDs - queryset = queryset.filter(id__in=downloaded_ids) - return queryset.order_by('-pin', '-created_at') @@ -191,6 +179,59 @@ class MiddleBookCollectionListView(ListAPIView): ).order_by('order') +class DownloadedBooksListView(ListAPIView): + """ + API view to list books that have been downloaded by the current user + """ + serializer_class = BookSerializer + permission_classes = (IsAuthenticated,) + filter_backends = [SearchFilter] + search_fields = ['title', 'summary', 'publisher', 'isbn'] + + @swagger_auto_schema( + operation_id="list_downloaded_books", + operation_description=""" + Retrieve a list of books that have been downloaded by the current user. + + This endpoint returns a paginated list of books that the authenticated user has downloaded. + The results are not cached to ensure real-time accuracy of the download list. + + You can search for downloaded books by title, summary, publisher, or ISBN using the 'search' query parameter. + """, + operation_summary="List Downloaded Books", + tags=["Library"], + manual_parameters=[ + openapi.Parameter( + 'search', + openapi.IN_QUERY, + description="Search downloaded books by title, summary, publisher, or ISBN", + type=openapi.TYPE_STRING, + required=False + ) + ], + responses={ + 200: "List of downloaded books with pagination", + 401: "Authentication credentials were not provided or are invalid", + 500: "Internal server error occurred" + } + ) + def get(self, request, *args, **kwargs): + return super().get(request, *args, **kwargs) + + def get_queryset(self): + # Get all downloaded book IDs for the current user + downloaded_ids = BookDownload.objects.filter( + user=self.request.user, + status=True + ).values_list('book_id', flat=True) + + # Return books that match these IDs + return Book.objects.filter( + id__in=downloaded_ids, + status=True + ).order_by('-created_at') + + class BookDownloadCreateAPIView(CreateAPIView): """ API view to create a book download record and increment the book's download count