Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified db.sqlite3
Binary file not shown.
Binary file added helloworld/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added helloworld/__pycache__/settings.cpython-311.pyc
Binary file not shown.
Binary file added helloworld/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added helloworld/__pycache__/wsgi.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/serializer.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added library/__pycache__/views.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 2 additions & 1 deletion library/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class AuthorSerializer(serializers.ModelSerializer):

class Meta:
model = Author
fields = '__all__'
fields = '__all__'

5 changes: 4 additions & 1 deletion library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
]
31 changes: 30 additions & 1 deletion library/views.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -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..

@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)
Binary file added tryHello/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added tryHello/__pycache__/admin.cpython-311.pyc
Binary file not shown.
Binary file added tryHello/__pycache__/apps.cpython-311.pyc
Binary file not shown.
Binary file added tryHello/__pycache__/models.cpython-311.pyc
Binary file not shown.
Binary file added tryHello/__pycache__/urls.cpython-311.pyc
Binary file not shown.
Binary file added tryHello/__pycache__/views.cpython-311.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.