55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
# app/Library/views.py
|
|
|
|
from django.http import JsonResponse
|
|
from rest_framework.views import APIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
from .serializers import BookSerializer
|
|
from .models import Book
|
|
|
|
|
|
def ping(request):
|
|
data = {"ping": "pong!"}
|
|
return JsonResponse(data)
|
|
|
|
|
|
class BookList(APIView):
|
|
def get(self, request):
|
|
books = Book.objects.all()
|
|
serializer = BookSerializer(books, many=True)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
|
|
def post(self, request):
|
|
serializer = BookSerializer(data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
class BookDetails(APIView):
|
|
def get(self, request, pk):
|
|
book = Book.objects.filter(pk=pk).first()
|
|
if book:
|
|
serializer = BookSerializer(book)
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|
|
|
|
def put(self, request, pk):
|
|
book = Book.objects.filter(pk=pk).first()
|
|
serializer = BookSerializer(book, data=request.data)
|
|
if serializer.is_valid():
|
|
serializer.save()
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
elif not book:
|
|
return Response(serializer.data, status=status.HTTP_404_NOT_FOUND)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
def delete(self, request, pk):
|
|
book = Book.objects.filter(pk=pk).first()
|
|
if book:
|
|
serializer = BookSerializer(book)
|
|
book.delete()
|
|
return Response(serializer.data, status=status.HTTP_200_OK)
|
|
return Response(status=status.HTTP_404_NOT_FOUND)
|