|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +from typing import Optional |
| 3 | + |
1 | 4 | from drf_yasg.utils import swagger_auto_schema |
2 | 5 | from rest_framework import viewsets, status |
3 | 6 | from rest_framework.permissions import IsAuthenticated |
|
6 | 9 | from api.common import ok |
7 | 10 | from api.common.response import make_response_serializer |
8 | 11 | from api.utils.common import with_common_response |
| 12 | +from common.responses import err |
9 | 13 | from common.serializers import PageQuerySerializer |
10 | 14 | from node.models import Node |
11 | 15 | from node.serializers import NodeList, NodeCreateBody, NodeID, NodeResponse |
@@ -53,3 +57,38 @@ def create(self, request): |
53 | 57 | status=status.HTTP_201_CREATED, |
54 | 58 | data=ok(NodeID(serializer.save().__dict__).data), |
55 | 59 | ) |
| 60 | + |
| 61 | + @swagger_auto_schema( |
| 62 | + operation_summary="Retrieve a node by ID", |
| 63 | + responses=with_common_response( |
| 64 | + {status.HTTP_200_OK: make_response_serializer(NodeResponse)} |
| 65 | + ), |
| 66 | + ) |
| 67 | + def retrieve(self, request, pk: Optional[str] = None): |
| 68 | + try: |
| 69 | + node = Node.objects.get(pk=pk, organization=request.user.organization) |
| 70 | + except Node.DoesNotExist: |
| 71 | + return Response( |
| 72 | + status=status.HTTP_404_NOT_FOUND, |
| 73 | + data=err("Node not found"), |
| 74 | + ) |
| 75 | + return Response( |
| 76 | + status=status.HTTP_200_OK, |
| 77 | + data=ok(NodeResponse(node, context={"organization": request.user.organization}).data), |
| 78 | + ) |
| 79 | + |
| 80 | + @swagger_auto_schema( |
| 81 | + operation_summary="Delete a node by ID", |
| 82 | + responses=with_common_response( |
| 83 | + {status.HTTP_204_NO_CONTENT: "No Content"} |
| 84 | + ), |
| 85 | + ) |
| 86 | + def destroy(self, request, pk: Optional[str] = None): |
| 87 | + try: |
| 88 | + Node.objects.get(pk=pk, organization=request.user.organization).delete() |
| 89 | + except Node.DoesNotExist: |
| 90 | + return Response( |
| 91 | + status=status.HTTP_404_NOT_FOUND, |
| 92 | + data=err("Node not found"), |
| 93 | + ) |
| 94 | + return Response(status=status.HTTP_204_NO_CONTENT) |
0 commit comments