def post(self, request): serializer = CompanyModelSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'error': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def put(self, request, company_id): company = self.get_object(company_id) serializer = CompanyModelSerializer(instance=company, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'error': serializer.errors})
def company_list(request): if request.method == 'GET': companies = Company.objects.all() serializer = CompanyModelSerializer(companies, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = CompanyModelSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response({'ERROR': serializer.errors}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def company_detail(request, company_id): try: company = Company.objects.get(id=company_id) except Company.DoesNotExist as e: return Response({'error': str(e)}) if request.method == 'GET': serializer = CompanyModelSerializer(company) return Response(serializer.data) elif request.method == 'PUT': serializer = CompanyModelSerializer(instance=company, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response({'errors':serializer.errors}) elif request.method == 'DELETE': company.delete() return Response({'deleted': True})