From 336c09504ece515d3c101f75a1f9654a854a050a Mon Sep 17 00:00:00 2001 From: Shreyas Myana Date: Sat, 5 Oct 2024 10:06:11 +0530 Subject: [PATCH] implement getByID for book and author --- .idea/dataSources.xml | 18 ++++++++++++++++++ .idea/helloworld.iml | 2 +- .idea/misc.xml | 2 +- db.sqlite3 | Bin 167936 -> 167936 bytes env/pyvenv.cfg | 8 ++++---- helloworld/settings.py | 1 + library/urls.py | 4 +++- library/views.py | 25 +++++++++++++++++++++++-- 8 files changed, 51 insertions(+), 9 deletions(-) diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index db6ba82c..523a0ce1 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -8,5 +8,23 @@ jdbc:sqlite:$PROJECT_DIR$/db.sqlite3 $ProjectFileDir$ + + sqlite.xerial + true + true + $PROJECT_DIR$/helloworld/settings.py + org.sqlite.JDBC + jdbc:sqlite:C:\Users\Shreyas\Documents\Scalar\Django\demoDjangoProject\db.sqlite3 + $ProjectFileDir$ + + + sqlite.xerial + true + true + $PROJECT_DIR$/helloworld/settings.py + org.sqlite.JDBC + jdbc:sqlite:C:\Users\Shreyas\Documents\Scalar\Django\demoDjangoProject\db.sqlite3 + $ProjectFileDir$ + \ No newline at end of file diff --git a/.idea/helloworld.iml b/.idea/helloworld.iml index b1023c6e..1304465d 100644 --- a/.idea/helloworld.iml +++ b/.idea/helloworld.iml @@ -14,7 +14,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 812ab5a6..7b668813 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/db.sqlite3 b/db.sqlite3 index 69bfcf1ff1f4a4b385c6eedfc18bb82676ff3b60..0b89e35c0267e3c7134b603486ebd0e9d3d1d6ad 100644 GIT binary patch delta 219 zcmZozz}2vTYl1YR#Y7orRtpBbvhIy3i{!=R8TnuG8}skxFXQj#pT!@?@5wLEzhbkZ z0yDpe5HGVPV@_sLQDRYLd}3)yMt%{~WMlnO2?0K4Rk%b_ettGnkvKE6G2`TeJi@Fz z3=9mS%{TPhZ|E~_zoE~hW5B`0|AvA82mhPRf(;M&6}Xs~8KjFdic%{Ri`f{N8H5v) zl6irenE0PE@PFigx>>N`CO;1s6AOblqZ80@L#F8;>zRasrhH@I|HJ>nn LMkXd^W=>83M*TZ9 delta 164 zcmZozz}2vTYl1YR*+dyiag1pR{oH?0EMTtd~@rk7+8Tm!x%*@7&lMnI;v+^)7Ft9Y=&~Lw?&$#`D zK9i0C8zcV@2L2zL1smS+3$ZaWGYBUpCG!H6G4g+8;Qt5|f6hPsV?C2FP~jg2{y#v0 N_x$WYd1hu#P5@CPE@=P& diff --git a/env/pyvenv.cfg b/env/pyvenv.cfg index 89c3f403..b383617c 100644 --- a/env/pyvenv.cfg +++ b/env/pyvenv.cfg @@ -1,5 +1,5 @@ -home = /opt/homebrew/opt/python@3.12/bin +home = C:\Users\Shreyas\AppData\Local\Programs\Python\Python312 include-system-site-packages = false -version = 3.12.4 -executable = /opt/homebrew/Cellar/python@3.12/3.12.4/Frameworks/Python.framework/Versions/3.12/bin/python3.12 -command = /opt/homebrew/opt/python@3.12/bin/python3.12 -m venv /Users/karan.bhatia/Desktop/personal/helloworld/env +version = 3.12.6 +executable = C:\Users\Shreyas\AppData\Local\Programs\Python\Python312\python.exe +command = C:\Users\Shreyas\AppData\Local\Programs\Python\Python312\python.exe -m venv C:\Users\Shreyas\Documents\Scalar\Django\demoDjangoProject\env diff --git a/helloworld/settings.py b/helloworld/settings.py index 4ada6bff..2470c846 100644 --- a/helloworld/settings.py +++ b/helloworld/settings.py @@ -37,6 +37,7 @@ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'rest_framework', 'tryHello', 'library' ] diff --git a/library/urls.py b/library/urls.py index b4361e4d..f52cf1d3 100644 --- a/library/urls.py +++ b/library/urls.py @@ -3,6 +3,8 @@ 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('book//', views.get_book_by_id, name='getBookByID'), + path('author//', views.get_author_by_id, name='getAuthorByID'), ] \ No newline at end of file diff --git a/library/views.py b/library/views.py index 052affad..ea26628b 100644 --- a/library/views.py +++ b/library/views.py @@ -2,8 +2,9 @@ from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response - +from .models import Book,Author from library.serializer import BookSerializer, AuthorSerializer +from rest_framework.exceptions import NotFound # Create your views here. @@ -32,4 +33,24 @@ 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,id): + try: + instance = Book.objects.get(pk=id) + except Book.DoesNotExist: + raise NotFound(f"No record found with ID {id}") + + serializer = BookSerializer(instance) + return Response(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,id): + try: + instance = Author.objects.get(pk=id) + except Author.DoesNotExist: + raise NotFound(f"No record found with ID {id}") + + serializer = AuthorSerializer(instance) + return Response(serializer.data, status=status.HTTP_200_OK) \ No newline at end of file