Commit f7da0abd authored by Ristylou Dolar's avatar Ristylou Dolar

Added APIServices CRUD

parent fd14d8de
......@@ -140,7 +140,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
......@@ -187,7 +187,7 @@ class ApplicationViewSet(viewsets.ModelViewSet):
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived marital status
# /archived - show list of archived application
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
......@@ -220,28 +220,258 @@ class ApplicationViewSet(viewsets.ModelViewSet):
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# Show app services
@action(methods=['GET'], detail=True)
def services(self, request, pk):
services = APIService.objects.filter(application=pk)
serializer = APIServiceSerializer(services, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
try:
services = APIService.objects.filter(application=pk)
if not services.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(services)
if page is not None:
serializer = APIServiceSerializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived applications found',
'results': serializer.data
}
return self.get_paginated_response(message)
# serializer = APIServiceSerializer(services, many=True)
# return Response(serializer.data, status=status.HTTP_200_OK)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
class APIServiceViewSet(viewsets.ModelViewSet):
http_method_names = [
'get', 'post', 'put', 'patch', 'head', 'options', 'trace'
'get', 'post', 'put', 'patch', 'delete', 'head', 'options', 'trace'
]
queryset = APIService.objects.all()
serializer_class = APIServiceSerializer
lookup_field = 'id'
lookup_field = 'pk'
pagination_class = CustomPagination
# CREATE Application
def create(self, request, *args, **kwargs):
try:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
headers = self.get_success_headers(serializer.data)
message = {
'code': 201,
'status': 'success',
'message': 'New service created',
'results': serializer.data
}
return Response(message)
except BadRequestException as e:
message = {
'code': 400,
'status': 'failed',
'message': '' + str(e),
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW LIST of Applications
def list(self, request, *args, **kwargs):
try:
queryset = APIService.objects.filter(deleted_at__exact=None)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'List of services found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SHOW application details
def retrieve(self, request, *args, **kwargs):
try:
id = self.kwargs['pk']
queryset = APIService.objects.filter(id=id)
serializer = self.get_serializer(queryset, many=True)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
else:
message = {
'code': 200,
'status': 'success',
'message': 'Service retrieved',
'results': serializer.data
}
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# UPDATE Application
def update(self, request, *args, **kwargs):
try:
partial = kwargs.pop('partial', False)
instance = self.get_object()
serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)
if getattr(instance, '_prefetched_objects_cache', None):
instance._prefetched_objects_cache = {}
message = {
'code': 200,
'status': 'success',
'message': 'Application updated',
'results': serializer.data
}
return Response(message)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# SOFT DELETE / ARCHIVED
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
self.perform_destroy(instance)
message = {
'code': 200,
'status': 'Success',
'message': 'Service deleted'
}
return Response(message, status=status.HTTP_200_OK)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# PATCH - RESTORE archived application
def partial_update(self, request, *args, **kwargs):
try:
kwargs['partial'] = True
instance = self.get_object()
instance.deleted_at = None
serializer = self.get_serializer(instance)
message = {
'code': 200,
'status': 'success',
'message': 'Archived service restored',
'results': serializer.data
}
instance.save()
return Response(message)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
# /archived - show list of archived application
@action(methods=["GET"], detail=False)
def archived(self, request, pk=None):
try:
queryset = APIService.objects.filter(deleted_at__isnull=False)
if not queryset.exists():
message = {
'code': 404,
'status': 'failed',
'message': 'No records found'
}
return Response(message, status=status.HTTP_404_NOT_FOUND)
page = self.paginate_queryset(queryset)
if page is not None:
serializer = self.get_serializer(page, many=True)
message = {
'code': 200,
'status': 'success',
'message': 'Archived services found',
'results': serializer.data
}
return self.get_paginated_response(message)
except Exception as e:
message = {
'code': 500,
'status': 'failed',
'message': 'Request was not able to process' + str(e),
}
return Response(message, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@action(methods=["GET"], detail=True)
def endpoints(self, request, id=None):
service = self.get_object()
endpoints = APIEndpoint.objects.filter(service=service)
serializer = APIEndpointSerializer(endpoints, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
# @action(methods=["GET"], detail=True)
# def endpoints(self, request, id=None):
# service = self.get_object()
# endpoints = APIEndpoint.objects.filter(service=service)
# serializer = APIEndpointSerializer(endpoints, many=True)
# return Response(serializer.data, status=status.HTTP_200_OK)
class APIEndpointViewSet(viewsets.ModelViewSet):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment