from django.http import HttpResponse
from django.http import Http404
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from .permissions import IsAdminOrReadOnly
from .models import Product
from .serializers import ProductSerializer


class ProductList(generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )


class ProductDetail(APIView):
    def get_object(self, pk):
        try:
            return Product.objects.get(pk=pk)
        except Product.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        product = self.get_object(pk)
        serializer = ProductSerializer(product)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        product = self.get_object(pk)
        serializer = ProductSerializer(product, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        product = self.get_object(pk)
        product.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

from rest_framework import generics
from .permissions import IsAdminOrReadOnly
from .models import Product
from .serializers import ProductSerializer


class ProductList(generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )


class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )

Implement ProductDetail with a generic RetrieveUpdateDestroyAPIView class
Implement ProductDetail with a generic RetrieveUpdateDestroyAPIView class

- from django.http import HttpResponse
- from django.http import Http404
  from rest_framework import generics
- from rest_framework.response import Response
- from rest_framework.views import APIView
- from rest_framework import status
  from .permissions import IsAdminOrReadOnly
  from .models import Product
  from .serializers import ProductSerializer
  
  
  class ProductList(generics.ListCreateAPIView):
      queryset = Product.objects.all()
      serializer_class = ProductSerializer
      permission_classes = (IsAdminOrReadOnly, )
  
  
+ class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
+     queryset = Product.objects.all()
+     serializer_class = ProductSerializer
+     permission_classes = (IsAdminOrReadOnly, )
- class ProductDetail(APIView):
-     def get_object(self, pk):
-         try:
-             return Product.objects.get(pk=pk)
-         except Product.DoesNotExist:
-             raise Http404
  
-     def get(self, request, pk, format=None):
-         product = self.get_object(pk)
-         serializer = ProductSerializer(product)
-         return Response(serializer.data)
- 
-     def put(self, request, pk, format=None):
-         product = self.get_object(pk)
-         serializer = ProductSerializer(product, data=request.data)
-         if serializer.is_valid():
-             serializer.save()
-             return Response(serializer.data)
-         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
- 
-     def delete(self, request, pk, format=None):
-         product = self.get_object(pk)
-         product.delete()
-         return Response(status=status.HTTP_204_NO_CONTENT)
- 
Implement ProductDetail with a generic RetrieveUpdateDestroyAPIView class
## Code Before:
from django.http import HttpResponse
from django.http import Http404
from rest_framework import generics
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from .permissions import IsAdminOrReadOnly
from .models import Product
from .serializers import ProductSerializer


class ProductList(generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )


class ProductDetail(APIView):
    def get_object(self, pk):
        try:
            return Product.objects.get(pk=pk)
        except Product.DoesNotExist:
            raise Http404

    def get(self, request, pk, format=None):
        product = self.get_object(pk)
        serializer = ProductSerializer(product)
        return Response(serializer.data)

    def put(self, request, pk, format=None):
        product = self.get_object(pk)
        serializer = ProductSerializer(product, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

    def delete(self, request, pk, format=None):
        product = self.get_object(pk)
        product.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

## Instruction:
Implement ProductDetail with a generic RetrieveUpdateDestroyAPIView class
## Code After:
from rest_framework import generics
from .permissions import IsAdminOrReadOnly
from .models import Product
from .serializers import ProductSerializer


class ProductList(generics.ListCreateAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )


class ProductDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    permission_classes = (IsAdminOrReadOnly, )
