Skip to content

Commit

Permalink
Merge pull request #16 from rkshaon/rkshaon
Browse files Browse the repository at this point in the history
backend
  • Loading branch information
rkshaon authored Sep 7, 2024
2 parents bdcdf1b + 47dab3e commit 58a3a32
Show file tree
Hide file tree
Showing 12 changed files with 82 additions and 5 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
1 change: 1 addition & 0 deletions backend/BookShelf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
path('admin/', admin.site.urls),
# app URLs
path('user/', include('user_api.urls')),
path('book/', include('book_api.urls')),
# Swagger documentation URLs
path('swagger/', schema_view.with_ui('swagger',
cache_timeout=0), name='schema-swagger-ui'),
Expand Down
20 changes: 20 additions & 0 deletions backend/BookShelf/utilities/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# BookShelf/utilities/pagination
from rest_framework.pagination import PageNumberPagination


class Pagination(PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100

def get_page_size(self, request):
"""
Handle dynamic page_size via query parameter
or fall back to default.
"""
if request.query_params.get(self.page_size_query_param):
return min(
int(request.query_params[self.page_size_query_param]),
self.max_page_size
)
return self.page_size
2 changes: 1 addition & 1 deletion backend/author_api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.shortcuts import render
# from django.shortcuts import render

# Create your views here.
Empty file.
9 changes: 9 additions & 0 deletions backend/book_api/serializers/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rest_framework import serializers

from book_api.models import Book


class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
7 changes: 7 additions & 0 deletions backend/book_api/urls/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path, include
from book_api.urls.v1 import urlpatterns as v1


urlpatterns = [
path('v1/', include(v1)),
]
9 changes: 9 additions & 0 deletions backend/book_api/urls/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path

from book_api.views import v1


urlpatterns = [
path('', v1.BookView.as_view()),
path('<int:pk>/', v1.BookView.as_view()),
]
3 changes: 0 additions & 3 deletions backend/book_api/views.py

This file was deleted.

Empty file.
33 changes: 33 additions & 0 deletions backend/book_api/views/v1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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

from book_api.models import Book

from book_api.serializers.v1 import BookSerializer


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)
return paginator.get_paginated_response(serializer.data)
2 changes: 1 addition & 1 deletion backend/publisher_api/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from django.shortcuts import render
# from django.shortcuts import render

# Create your views here.

0 comments on commit 58a3a32

Please sign in to comment.