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
1 change: 1 addition & 0 deletions library/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
path('createbook/', views.create_book, name='createbook'),
path('createAuthor/', views.create_author, name='createauthor'),
path('get_book_by_id/<int:book_id>', views.get_book_by_id, name='get_book_by_id'),
path('get_book_by_author/<int:author_id>', views.get_book_by_author, name='get_author_by_id'),
]
15 changes: 14 additions & 1 deletion library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from rest_framework.decorators import api_view, action
from rest_framework.response import Response

from library.models import Book
from library.models import Book, Author
from library.serializer import BookSerializer, AuthorSerializer


Expand Down Expand Up @@ -34,6 +34,7 @@ def create_author(request):

# TODO: GET BOOK BY ID and return json response..


@api_view(['GET'])
def get_book_by_id(request, book_id):
try:
Expand All @@ -43,5 +44,17 @@ def get_book_by_id(request, book_id):

except Book.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
#http://localhost:8000/api/get_book_by_id


# TODO: GET AUTHOR BY ID and return json response..
@api_view(['GET'])
def get_book_by_author(request, author_id):
try:
author = Author.objects.get(pk=author_id)
author_serializer = AuthorSerializer(author)
return Response(author_serializer.data, status=status.HTTP_200_OK)

except Author.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)