Skip to content

Commit

Permalink
Merged functions to one GET and POST func
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloj2001 committed Nov 24, 2023
1 parent 8c70ce9 commit a77fb02
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
3 changes: 1 addition & 2 deletions api/optimeet/groups/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
#path('usergroups/', views.UserGroupListAPIView.as_view(), name='usergroup-list'),#get all usergroup
#path('usergroups/<str:user_id>/', views.UserGroupListAPIView.as_view(), name='usergroup-byuser'),#get usergroup by user_id
path("<str:group_id>/recs/", views.get_recommendation),
path("<str:group_id>/votes/", views.votes, name='votes'),
path("<str:group_id>/votes/", views.create_vote, name='create_vote')
path("<str:group_id>/votes/", views.votes, name='votes')
]
34 changes: 16 additions & 18 deletions api/optimeet/groups/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,25 @@ def add_preference_to_group(request, group_id, user_id, format=None):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

@csrf_exempt
@api_view(['GET'])
@api_view(['GET', 'POST'])
def votes(request, group_id):
votes = models.Votes.objects.filter(group_id=group_id)
serializer = serializers.VotesSerializer(votes, many=True)
return Response(serializer.data)

@csrf_exempt
@api_view(['POST'])
def create_vote(request, group_id):
try:
group = models.Group.objects.get(pk=group_id)
except models.Group.DoesNotExist:
raise Http404
if request.method == 'GET':
votes = models.Votes.objects.filter(group_id=group_id)
serializer = serializers.VotesSerializer(votes, many=True)
return Response(serializer.data)
elif request.method == 'POST':
try:
group = models.Group.objects.get(pk=group_id)
except models.Group.DoesNotExist:
raise Http404

serializer = serializers.VotesSerializer(data = request.data, context={'group_id': group})
serializer = serializers.VotesSerializer(data = request.data, context={'group_id': group})

if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_201_CREATED)

return Response(status=status.HTTP_400_BAD_REQUEST)
if serializer.is_valid():
serializer.save()
return Response(status=status.HTTP_201_CREATED)
return Response(status=status.HTTP_400_BAD_REQUEST)

# #for testing
# class UserGroupListAPIView(APIView):
Expand Down

0 comments on commit a77fb02

Please sign in to comment.