Skip to content

Commit

Permalink
Merge pull request #100 from codersforcauses/issue-99-Fix_further_iss…
Browse files Browse the repository at this point in the history
…ues_with_RSVP_endpoint

Issue 99 fix further issues with rsvp endpoint
  • Loading branch information
harryrigg authored Jul 26, 2024
2 parents eec6f0d + 86a2cca commit 05d5b40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
9 changes: 4 additions & 5 deletions server/api/event/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
router.register(r"", views.EventViewSet, basename="event")

urlpatterns = [
path('', include(router.urls)),
path('<int:event_id>/rsvp/', views.rsvp_list_create,
name='rsvp-list-create'),
path('<int:event_id>/rsvp/<int:id>/', views.rsvp_detail,
name='rsvp-detail'),
path("", include(router.urls)),
path("<int:event_id>/rsvp/", views.rsvp_event_view, name="rsvp-event-view"),
path("<int:event_id>/rsvp/<int:id>/", views.rsvp_detail, name="rsvp-detail"),
path("<int:event_id>/has_rsvp/", views.has_rsvp, name="has-rsvp"),
]
31 changes: 24 additions & 7 deletions server/api/event/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db import IntegrityError
from django.http import JsonResponse
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import viewsets
Expand Down Expand Up @@ -39,20 +40,36 @@ class EventViewSet(viewsets.ModelViewSet):
permission_classes = [isStaffOrReadonly]


@api_view(["GET", "POST"])
@api_view(["GET", "POST", "DELETE"])
@permission_classes([IsAuthenticated])
def rsvp_list_create(request: HttpRequest, event_id):
def rsvp_event_view(request: HttpRequest, event_id):
if request.method == "GET":
rsvps = RSVP.objects.filter(event__id=event_id)
serializer = RSVPSerializer(rsvps, many=True)
return Response(serializer.data)

elif request.method == "POST":
serializer = RSVPSerializer(data={"event": event_id, "user": request.user.id})
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
rsvp = RSVP(event_id=event_id, user=request.user)
try:
rsvp.save()
except IntegrityError:
return Response(status=status.HTTP_409_CONFLICT)
except Exception:
return Response(status=status.HTTP_400_BAD_REQUEST)

# Return data
response_data = {}
response_data["rsvp_id"] = rsvp.id
return Response(response_data, status=status.HTTP_201_CREATED)

elif request.method == "DELETE":
try:
rsvp = RSVP.objects.get(event_id=event_id, user=request.user)
except ObjectDoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)

rsvp.delete()
return Response(status=status.HTTP_200_OK)


@api_view(["GET", "PATCH", "DELETE"])
Expand Down

0 comments on commit 05d5b40

Please sign in to comment.