from django.shortcuts import render

# Create your views here.
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from . models import employees
from . serializers import employeesSerializer, employeesListSerializer

class employeeList(APIView):

    def get(self, request):
        employeesObj = employees.objects.all()
        serializer = employeesSerializer(employeesObj, many = True)
        return Response(serializer.data)

    def post(self, request, format=None):
        serializer = employeesSerializer(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 employeeListView(APIView):

    def get(self, request):
        employeesObj = [employees.objects.latest('firstname')]
        serializer = employeesListSerializer(employeesObj, many = True)
        return Response(serializer.data)
