diff --git a/library/urls.py b/library/urls.py index b4361e4d..68beccf8 100644 --- a/library/urls.py +++ b/library/urls.py @@ -5,4 +5,6 @@ urlpatterns = [ path('createbook/', views.create_book, name='createbook'), path('createAuthor/', views.create_author, name='createauthor'), + path('getBook/', views.get_book, name='getbook'), + path('getAuthor/', views.get_author, name='getauthor'), ] \ No newline at end of file diff --git a/library/views.py b/library/views.py index 4f40abfa..bf1a14a4 100644 --- a/library/views.py +++ b/library/views.py @@ -3,6 +3,7 @@ from rest_framework.decorators import api_view from rest_framework.response import Response +from library.models import Author, Book from library.serializer import BookSerializer, AuthorSerializer @@ -31,4 +32,17 @@ def create_author(request): return Response(author_serializer.errors, status=status.HTTP_400_BAD_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 +#127.0.0.1:8000/api/getBook?id=1 +@api_view(['GET']) +def get_book(request): + index = request.query_params.get('id') + book_serializer = BookSerializer(Book.objects.get(pk=index)) + return Response(book_serializer.data, status=status.HTTP_200_OK) + +#127.0.0.1:8000/api/getAuthor?id=1 +# TODO: GET AUTHOR BY ID and return json response.. +@api_view(['GET']) +def get_author(request): + index = request.query_params.get('id') + author_serializer = AuthorSerializer(Author.objects.get(pk=index)) + return Response(author_serializer.data, status=status.HTTP_200_OK) \ No newline at end of file