from rest_framework import generics, status
from rest_framework.decorators import api_view
from rest_framework.response import Response

from .serializers import UserSerializer
from .models import User


# ListCreateAPIView is a generic view which provides `GET` and `POST` method handlers
class CreateView(generics.ListCreateAPIView):
    """This class defines the create behavior of rest api."""
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new User."""
        serializer.save()


# RetrieveUpdateDestroyAPIView is a generic view that provides `GET(one)`, `PUT`, `PATCH` and `DELETE` method handlers
class DetailsView(generics.RetrieveUpdateDestroyAPIView):
    """This class handles the HTTP GET, PUT, and DELETE requests."""
    queryset = User.objects.all()
    serializer_class = UserSerializer


# Method usage
# @api_view(['GET', 'POST'])
# def user_list(request):
#     if request.method == 'GET':
#         users = User.objects.all()
#         serializer = UserSerializer(users, many=True)
#         return Response(serializer.data)
#     if request.method == 'POST':
#         serializer = UserSerializer(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)
#
#
# @api_view(['GET', 'PUT', 'PATCH', 'DELETE'])
# def user_detail(request, pk):
#     try:
#         user = User.objects.get(pk=pk)
#     except User.DoesNotExist:
#         return Response(status=status.HTTP_404_NOT_FOUND)
#
#     if request.method == 'GET':
#         serializer = UserSerializer(user)
#         return Response(serializer.data)
#     if request.method == 'PUT' or request.method == 'PATCH':
#         serializer = UserSerializer(user, data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     if request.method == 'DELETE':
#         user.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)

from rest_framework import generics, status
from rest_framework.decorators import api_view
from rest_framework.response import Response

from .serializers import UserSerializer
from .models import User


# ListCreateAPIView is a generic view which provides `GET` and `POST` method handlers
class CreateView(generics.ListCreateAPIView):
    """This class defines the create behavior of rest api."""
    queryset = User.objects.all()
    serializer_class = UserSerializer

    def perform_create(self, serializer):
        """Save the post data when creating a new User."""
        serializer.save()


# RetrieveUpdateDestroyAPIView is a generic view that provides `GET(one)`, `PUT`, `PATCH` and `DELETE` method handlers
class DetailsView(generics.RetrieveUpdateDestroyAPIView):
    """This class handles the HTTP GET, PUT, and DELETE requests."""
    queryset = User.objects.all()
    serializer_class = UserSerializer


# Method usage
# @api_view(['GET', 'POST'])
# def user_list(request):
#     if request.method == 'GET':
#         users = User.objects.all()
#         serializer = UserSerializer(users, many=True)
#         return Response(serializer.data)
#     if request.method == 'POST':
#         serializer = UserSerializer(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)
#
#
# @api_view(['GET', 'PUT', 'PATCH', 'DELETE'])
# def user_detail(request, pk):
#     try:
#         user = User.objects.get(pk=pk)
#     except User.DoesNotExist:
#         return Response(status=status.HTTP_404_NOT_FOUND)
#
#     if request.method == 'GET':
#         serializer = UserSerializer(user)
#         return Response(serializer.data)
#     if request.method == 'PUT' or request.method == 'PATCH':
#         serializer = UserSerializer(user, data=request.data)
#         if serializer.is_valid():
#             serializer.save()
#             return Response(serializer.data)
#         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
#     if request.method == 'DELETE':
#         user.delete()
#         return Response(status=status.HTTP_204_NO_CONTENT)

# ListCreateAPIView is a generic view which provides `GET` and `POST` method handlers This class defines the create behavior of rest api. Save the post data when creating a new User. # RetrieveUpdateDestroyAPIView is a generic view that provides `GET(one)`, `PUT`, `PATCH` and `DELETE` method handlers This class handles the HTTP GET, PUT, and DELETE requests. # Method usage # @api_view(['GET', 'POST']) # def user_list(request): #     if request.method == 'GET': #         users = User.objects.all() #         serializer = UserSerializer(users, many=True) #         return Response(serializer.data) #     if request.method == 'POST': #         serializer = UserSerializer(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) # # # @api_view(['GET', 'PUT', 'PATCH', 'DELETE']) # def user_detail(request, pk): #     try: #         user = User.objects.get(pk=pk) #     except User.DoesNotExist: #         return Response(status=status.HTTP_404_NOT_FOUND) # #     if request.method == 'GET': #         serializer = UserSerializer(user) #         return Response(serializer.data) #     if request.method == 'PUT' or request.method == 'PATCH': #         serializer = UserSerializer(user, data=request.data) #         if serializer.is_valid(): #             serializer.save() #             return Response(serializer.data) #         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) #     if request.method == 'DELETE': #         user.delete() #         return Response(status=status.HTTP_204_NO_CONTENT)