Skip to content

Commit

Permalink
improved clients api
Browse files Browse the repository at this point in the history
Signed-off-by: Trey <[email protected]>
  • Loading branch information
TreyWW committed Jun 20, 2024
1 parent b3dcbae commit b5d8a54
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 6 deletions.
52 changes: 49 additions & 3 deletions backend/api/public/endpoints/clients/delete.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Literal

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand All @@ -7,12 +10,55 @@
from backend.service.clients.delete import delete_client


@swagger_auto_schema(
method="delete",
operation_description="Delete a client",
operation_id="clients_delete",
responses={
200: openapi.Response(
description="Client deleted successfully",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"success": openapi.Schema(type=openapi.TYPE_BOOLEAN, description="Indicates if the operation was successful"),
"client_id": openapi.Schema(type=openapi.TYPE_STRING, description="The ID of the deleted client"),
},
),
),
403: openapi.Response(
description="Forbidden",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"success": openapi.Schema(type=openapi.TYPE_BOOLEAN, description="Indicates if the operation was successful"),
"message": openapi.Schema(type=openapi.TYPE_STRING, description="You do not have permission to delete this client"),
},
),
),
404: openapi.Response(
description="Not Found",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"success": openapi.Schema(type=openapi.TYPE_BOOLEAN, description="Indicates if the operation was successful"),
"message": openapi.Schema(type=openapi.TYPE_STRING, description="This client does not exist"),
},
),
examples={
"application/json": {
"success": False,
"message": "This client does not exist",
}
},
),
},
)
@api_view(["DELETE"])
@authentication_classes([BearerAuthentication])
@permission_classes([IsAuthenticated])
def client_delete_endpoint(request, client_id):
def client_delete_endpoint(request, client_id: int):
response: str | Literal[True] = delete_client(request, client_id)

if isinstance(response, str):
return Response({"success": False, "message": response})
return Response({"success": True, "client_id": client_id})
return Response({"success": False, "message": response}, status=403 if "do not have permission" in response else 404)
return Response({"success": True, "client_id": client_id}, status=200)
30 changes: 30 additions & 0 deletions backend/api/public/endpoints/clients/list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
from django.db.models import QuerySet
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

Expand All @@ -9,14 +12,41 @@
from backend.service.clients.get import fetch_clients


@swagger_auto_schema(
method="get",
operation_description="List all clients",
operation_id="clients_list",
manual_parameters=[
openapi.Parameter("order_by", openapi.IN_QUERY, description="field you want to order by to", type=openapi.TYPE_STRING),
openapi.Parameter("search", openapi.IN_QUERY, description="field you want to search by", type=openapi.TYPE_STRING),
],
responses={
200: openapi.Response(
description="List of clients",
schema=openapi.Schema(
type=openapi.TYPE_OBJECT,
properties={
"success": openapi.Schema(type=openapi.TYPE_BOOLEAN),
"clients": openapi.Schema(type=openapi.TYPE_ARRAY, items=openapi.Items(type=openapi.TYPE_OBJECT)),
},
),
)
},
)
@api_view(["GET"])
@authentication_classes([BearerAuthentication])
@permission_classes([IsAuthenticated])
def list_clients_endpoint(request):

# paginator = PageNumberPagination()
# paginator.page_size = 5

search_text = request.GET.get("search")

clients: QuerySet[Client] = fetch_clients(request, search_text=search_text)

# queryset = paginator.paginate_queryset(clients, request)

serializer = ClientSerializer(clients, many=True)

return Response({"success": True, "clients": serializer.data})
2 changes: 1 addition & 1 deletion backend/api/public/endpoints/clients/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
list.list_clients_endpoint,
name="list",
),
path("delete", delete.client_delete_endpoint, name="delete"),
path("<int:id>/delete/", delete.client_delete_endpoint, name="delete"),
]

app_name = "clients"
2 changes: 0 additions & 2 deletions settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@
"webhook:create_schedule",
]

# @login_required()

LOGIN_REQUIRED_IGNORE_PATHS = [
r"^/favicon\.ico$",
r"^/static/(.*)/",
Expand Down

0 comments on commit b5d8a54

Please sign in to comment.