|
@@ -0,0 +1,30 @@
|
|
|
|
+from django.db.models import CharField
|
|
|
|
+from django.db.models.functions import Cast
|
|
|
|
+from rest_framework import viewsets, permissions
|
|
|
|
+from rest_framework.response import Response
|
|
|
|
+from dbsynce.serializer import FavoritecontactsSerializer
|
|
|
|
+from dbsynce.models import Favoritecontacts
|
|
|
|
+
|
|
|
|
+class FavoritecontactsMVS(viewsets.ModelViewSet):
|
|
|
|
+ queryset = Favoritecontacts.objects.all()
|
|
|
|
+ serializer_class = FavoritecontactsSerializer
|
|
|
|
+ permission_classes = [permissions.IsAuthenticated]
|
|
|
|
+
|
|
|
|
+ def list(self, request, *args, **kwargs):
|
|
|
|
+ client_id = self.request.query_params.get('client_id')
|
|
|
|
+ queryset = Favoritecontacts.objects.all()
|
|
|
|
+ if client_id is not None:
|
|
|
|
+ queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
|
|
|
|
+ serializer = self.get_serializer(queryset, many=True)
|
|
|
|
+ return Response(serializer.data)
|
|
|
|
+
|
|
|
|
+ def delete(self, request, *args, **kwargs):
|
|
|
|
+ client_id = self.request.query_params.get('client_id')
|
|
|
|
+ queryset = Favoritecontacts.objects.all()
|
|
|
|
+ if not client_id:
|
|
|
|
+ return Response({'error': 'client_id parameter is required'}, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
+
|
|
|
|
+ queryset = queryset.annotate(client_id_int=Cast('client_id', output_field=CharField())).filter(client_id_int=client_id)
|
|
|
|
+ queryset.delete()
|
|
|
|
+
|
|
|
|
+ return Response({'message': f'Objects with client_id {client_id} were deleted'}, status=status.HTTP_204_NO_CONTENT)
|