Skip to content

Commit

Permalink
upd base
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaakgul committed Dec 24, 2023
1 parent 5cc4f86 commit 0a4954f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
4 changes: 3 additions & 1 deletion apps/common/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ def trigger_error(request):
division_by_zero = 1 / 0


@api_view(['GET'])
def health_check(request):
return HttpResponse("Health Check Status: Stable", status=200)
return Response({'message': 'Hello, world!'}, status=200)
#return HttpResponse("Health Check Status: Stable", status=200)


def trigger_error_exception(request):
Expand Down
2 changes: 2 additions & 0 deletions apps/links/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
)
from apps.links.api.views import (
ListCustomLinkView,
ListCreateCustomLinkView
)


Expand All @@ -13,4 +14,5 @@
urlpatterns = [
path('tags', TagView.as_view(), name='list'),
path('custom-links', ListCustomLinkView.as_view(), name='custom-link-list'),
path('custom-links-generics', ListCreateCustomLinkView.as_view(), name='custom-link-generics'),
]
63 changes: 63 additions & 0 deletions apps/links/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from apps.tags.models import Tag
from apps.links.models import CustomLink
from rest_framework.generics import get_object_or_404, ListAPIView
from rest_framework.views import APIView
from rest_framework import authentication, permissions
from rest_framework.response import Response
from rest_framework import status
from rest_framework.decorators import api_view

from apps.tags.api.serializers import TagSerializer
from apps.links.api.serializers import CustomLinkSerializer
Expand All @@ -22,3 +27,61 @@ class ListCustomLinkView(ListAPIView):

def get_queryset(self):
return CustomLink.objects.filter(user=self.request.user)


# Class Based API Views
class ListCreateCustomLinkView(APIView):
"""
View to list all users in the system.
* Requires token authentication.
* Only authenticated users are able to access this view.
"""
authentication_classes = [authentication.TokenAuthentication]
permission_classes = [permissions.IsAuthenticated]

def get(self, request, format=None):
"""
Return a list of all custom links.
"""
custom_links = CustomLink.objects.filter(user=self.request.user)
serializer = CustomLinkSerializer(custom_links, many=True)
return Response(serializer.data)

def post(self, request, format=None):
"""
Create a new custom link.
"""
serializer = CustomLinkSerializer(data=request.data)
if serializer.is_valid():
serializer.save(user=self.request.user)
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


@api_view(['GET', 'PUT', 'DELETE'])
def custom_link_detail(request, pk):
"""
Retrieve, update or delete a custom link.
"""
custom_link = get_object_or_404(CustomLink, pk=pk)
# OR
# try:
# custom_link = CustomLink.objects.get(pk=pk)
# except CustomLink.DoesNotExist:
# return Response(status=status.HTTP_404_NOT_FOUND)
if request.method == 'GET':
serializer = CustomLinkSerializer(custom_link)
return Response(serializer.data)

elif request.method == 'PUT':
serializer = CustomLinkSerializer(custom_link, data=request.data)
if serializer.is_valid():
serializer.save(user=request.user)
return Response(serializer.data, status=status.HTTP_200_OK)
# return Response(status=status.HTTP_204_NO_CONTENT
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == 'DELETE':
custom_link.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

0 comments on commit 0a4954f

Please sign in to comment.