Skip to content

Commit

Permalink
backend: get single book api done
Browse files Browse the repository at this point in the history
  • Loading branch information
rkshaon committed Sep 7, 2024
1 parent c47dfed commit 47dab3e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions backend/BookShelf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,4 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
AUTH_USER_MODEL = 'user_api.User'
APPEND_SLASH = True
3 changes: 2 additions & 1 deletion backend/book_api/urls/v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@


urlpatterns = [
path('', v1.BookView.as_view())
path('', v1.BookView.as_view()),
path('<int:pk>/', v1.BookView.as_view()),
]
14 changes: 13 additions & 1 deletion backend/book_api/views/v1.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from rest_framework.exceptions import NotFound

from BookShelf.utilities.pagination import Pagination

Expand All @@ -12,9 +14,19 @@ class BookView(APIView):
permission_classes = [AllowAny]

def get(self, request, *args, **kwargs):
pk = kwargs.get('pk', None)

if pk:
try:
book = Book.objects.get(pk=pk, is_deleted=False)
except Book.DoesNotExist:
raise NotFound(detail="Book not found.")

return Response(BookSerializer(book).data)

books = Book.objects.filter(
is_deleted=False
)
).order_by('id')
paginator = Pagination()
page = paginator.paginate_queryset(books, request)
serializer = BookSerializer(page, many=True)
Expand Down

0 comments on commit 47dab3e

Please sign in to comment.