From 740f3f0424233ffdf43b2b2366d65a83597f7fef Mon Sep 17 00:00:00 2001 From: VinodRajN91 Date: Fri, 1 Nov 2024 15:07:35 +0530 Subject: [PATCH] adding get_author_by_id_service --- library/urls.py | 1 + library/views.py | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/library/urls.py b/library/urls.py index d3d32f5..5c78941 100644 --- a/library/urls.py +++ b/library/urls.py @@ -6,4 +6,5 @@ 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_book_by_author/', views.get_book_by_author, name='get_author_by_id'), ] \ No newline at end of file diff --git a/library/views.py b/library/views.py index a5b4d84..1498964 100644 --- a/library/views.py +++ b/library/views.py @@ -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 @@ -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: @@ -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) +