diff --git a/db.sqlite3 b/db.sqlite3 index 69bfcf1f..9500e48c 100644 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/helloworld/__pycache__/__init__.cpython-311.pyc b/helloworld/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..dac5ba03 Binary files /dev/null and b/helloworld/__pycache__/__init__.cpython-311.pyc differ diff --git a/helloworld/__pycache__/settings.cpython-311.pyc b/helloworld/__pycache__/settings.cpython-311.pyc new file mode 100644 index 00000000..3fc93830 Binary files /dev/null and b/helloworld/__pycache__/settings.cpython-311.pyc differ diff --git a/helloworld/__pycache__/urls.cpython-311.pyc b/helloworld/__pycache__/urls.cpython-311.pyc new file mode 100644 index 00000000..4eebd902 Binary files /dev/null and b/helloworld/__pycache__/urls.cpython-311.pyc differ diff --git a/helloworld/__pycache__/wsgi.cpython-311.pyc b/helloworld/__pycache__/wsgi.cpython-311.pyc new file mode 100644 index 00000000..d13d0209 Binary files /dev/null and b/helloworld/__pycache__/wsgi.cpython-311.pyc differ diff --git a/library/__pycache__/__init__.cpython-311.pyc b/library/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..f13c643d Binary files /dev/null and b/library/__pycache__/__init__.cpython-311.pyc differ diff --git a/library/__pycache__/admin.cpython-311.pyc b/library/__pycache__/admin.cpython-311.pyc new file mode 100644 index 00000000..ce815d85 Binary files /dev/null and b/library/__pycache__/admin.cpython-311.pyc differ diff --git a/library/__pycache__/apps.cpython-311.pyc b/library/__pycache__/apps.cpython-311.pyc new file mode 100644 index 00000000..9342e93d Binary files /dev/null and b/library/__pycache__/apps.cpython-311.pyc differ diff --git a/library/__pycache__/models.cpython-311.pyc b/library/__pycache__/models.cpython-311.pyc new file mode 100644 index 00000000..abee28d8 Binary files /dev/null and b/library/__pycache__/models.cpython-311.pyc differ diff --git a/library/__pycache__/serializer.cpython-311.pyc b/library/__pycache__/serializer.cpython-311.pyc new file mode 100644 index 00000000..ca110abb Binary files /dev/null and b/library/__pycache__/serializer.cpython-311.pyc differ diff --git a/library/__pycache__/urls.cpython-311.pyc b/library/__pycache__/urls.cpython-311.pyc new file mode 100644 index 00000000..4681cf2c Binary files /dev/null and b/library/__pycache__/urls.cpython-311.pyc differ diff --git a/library/__pycache__/views.cpython-311.pyc b/library/__pycache__/views.cpython-311.pyc new file mode 100644 index 00000000..fb0289c3 Binary files /dev/null and b/library/__pycache__/views.cpython-311.pyc differ diff --git a/library/migrations/__pycache__/0001_initial.cpython-311.pyc b/library/migrations/__pycache__/0001_initial.cpython-311.pyc new file mode 100644 index 00000000..76340d44 Binary files /dev/null and b/library/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/library/migrations/__pycache__/__init__.cpython-311.pyc b/library/migrations/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..9e47cb9b Binary files /dev/null and b/library/migrations/__pycache__/__init__.cpython-311.pyc differ diff --git a/library/serializer.py b/library/serializer.py index 53d46875..b894676f 100644 --- a/library/serializer.py +++ b/library/serializer.py @@ -13,4 +13,5 @@ class AuthorSerializer(serializers.ModelSerializer): class Meta: model = Author - fields = '__all__' \ No newline at end of file + fields = '__all__' + diff --git a/library/urls.py b/library/urls.py index b4361e4d..c961102e 100644 --- a/library/urls.py +++ b/library/urls.py @@ -3,6 +3,9 @@ from library import views urlpatterns = [ - path('createbook/', views.create_book, name='createbook'), + path('createBook/', views.create_book, name='createbook'), path('createAuthor/', views.create_author, name='createauthor'), + path('get_book_by_id/',views.get_book_by_id,name='get_book_by_id'), + + path('get_author_by_id/', views.get_author_by_id, name='get_author_by_id') ] \ No newline at end of file diff --git a/library/views.py b/library/views.py index 052affad..24881db3 100644 --- a/library/views.py +++ b/library/views.py @@ -1,8 +1,10 @@ from django.shortcuts import render from rest_framework import status from rest_framework.decorators import api_view +from rest_framework.generics import get_object_or_404 from rest_framework.response import Response +from library.models import Book,Author from library.serializer import BookSerializer, AuthorSerializer @@ -32,4 +34,31 @@ def create_author(request): # TODO: GET BOOK BY ID and return json response.. -# TODO: GET AUTHOR BY ID and return json response.. \ No newline at end of file + +@api_view(['GET']) +def get_book_by_id(request): + book_id = request.query_params.get('book_id') + + # If no book_id is provided, return bad request + if not book_id: + return Response({"error": "book_id is required"}, status=status.HTTP_400_BAD_REQUEST) + + book=get_object_or_404(Book,id=book_id) + book_serializer = BookSerializer(book) + + return Response(book_serializer.data, status=status.HTTP_200_OK) + +# TODO: GET AUTHOR BY ID and return json response.. + + +@api_view(['GET']) +def get_author_by_id(request): + author_id = request.query_params.get('author_id') + + # If no book_id is provided, return bad request + if not author_id: + return Response({"error": "author_id is required"}, status=status.HTTP_400_BAD_REQUEST) + book = get_object_or_404(Author, id=author_id) + author_serializer =AuthorSerializer(book) + + return Response(author_serializer.data, status=status.HTTP_200_OK) diff --git a/tryHello/__pycache__/__init__.cpython-311.pyc b/tryHello/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..bb336970 Binary files /dev/null and b/tryHello/__pycache__/__init__.cpython-311.pyc differ diff --git a/tryHello/__pycache__/admin.cpython-311.pyc b/tryHello/__pycache__/admin.cpython-311.pyc new file mode 100644 index 00000000..d772efeb Binary files /dev/null and b/tryHello/__pycache__/admin.cpython-311.pyc differ diff --git a/tryHello/__pycache__/apps.cpython-311.pyc b/tryHello/__pycache__/apps.cpython-311.pyc new file mode 100644 index 00000000..31243ea6 Binary files /dev/null and b/tryHello/__pycache__/apps.cpython-311.pyc differ diff --git a/tryHello/__pycache__/models.cpython-311.pyc b/tryHello/__pycache__/models.cpython-311.pyc new file mode 100644 index 00000000..16f473f9 Binary files /dev/null and b/tryHello/__pycache__/models.cpython-311.pyc differ diff --git a/tryHello/__pycache__/urls.cpython-311.pyc b/tryHello/__pycache__/urls.cpython-311.pyc new file mode 100644 index 00000000..d4782e56 Binary files /dev/null and b/tryHello/__pycache__/urls.cpython-311.pyc differ diff --git a/tryHello/__pycache__/views.cpython-311.pyc b/tryHello/__pycache__/views.cpython-311.pyc new file mode 100644 index 00000000..3d88cf13 Binary files /dev/null and b/tryHello/__pycache__/views.cpython-311.pyc differ diff --git a/tryHello/migrations/__pycache__/0001_initial.cpython-311.pyc b/tryHello/migrations/__pycache__/0001_initial.cpython-311.pyc new file mode 100644 index 00000000..03a1a769 Binary files /dev/null and b/tryHello/migrations/__pycache__/0001_initial.cpython-311.pyc differ diff --git a/tryHello/migrations/__pycache__/0002_enrollment_is_active.cpython-311.pyc b/tryHello/migrations/__pycache__/0002_enrollment_is_active.cpython-311.pyc new file mode 100644 index 00000000..7a164740 Binary files /dev/null and b/tryHello/migrations/__pycache__/0002_enrollment_is_active.cpython-311.pyc differ diff --git a/tryHello/migrations/__pycache__/0003_testtable.cpython-311.pyc b/tryHello/migrations/__pycache__/0003_testtable.cpython-311.pyc new file mode 100644 index 00000000..2d6704d7 Binary files /dev/null and b/tryHello/migrations/__pycache__/0003_testtable.cpython-311.pyc differ diff --git a/tryHello/migrations/__pycache__/__init__.cpython-311.pyc b/tryHello/migrations/__pycache__/__init__.cpython-311.pyc new file mode 100644 index 00000000..2ea46b73 Binary files /dev/null and b/tryHello/migrations/__pycache__/__init__.cpython-311.pyc differ