Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 99 fix further issues with rsvp endpoint #100

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading