Skip to content
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
8 changes: 8 additions & 0 deletions backend/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from hoagiehelp.api.answer_views import AnswerDetailView, AnswerListView
from hoagiehelp.api.comment_views import CommentDetailView, CommentListView
from hoagiehelp.api.question_views import QuestionDetailView, QuestionListView
from backend.hoagiehelp.api.study_group_views import StudyGroupDetailView, StudyGroupListView
from hoagiehelp.api.user_views import (
UserView,
user_answers,
Expand Down Expand Up @@ -52,6 +53,13 @@
path(
"comments/<str:comment_id>/", CommentDetailView.as_view(), name="comment-detail"
),
# Study Groups
path("study-groups/", StudyGroupListView.as_view(), name="studygroup-list"),
path(
"study-groups/<str:studygroup_id>/",
StudyGroupDetailView.as_view(),
name="studygroup-detail",
),
# Users
path("users/<str:user_id>/", UserView.as_view(), name="user-detail"),
path("users/<str:user_id>/questions/", user_questions, name="user-questions"),
Expand Down
80 changes: 80 additions & 0 deletions backend/hoagiehelp/api/study_group_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from rest_framework import serializers, status
from rest_framework.response import Response
from rest_framework.views import APIView

from hoagiehelp.models.studygroup import StudyGroup


# Study Group Serializer
class StudyGroupSerializer(serializers.ModelSerializer):
class Meta:
model = StudyGroup
fields = (
"id",
"title",
"description",
"leader",
"meeting_datetime",
"max_spots",
"members",
"created_at",
"updated_at",
)


class StudyGroupListView(APIView):
"""Handle collection operations for study groups."""

def get(self, request) -> Response:
"""List all study groups."""
queryset = StudyGroup.objects.all()
serializer = StudyGroupSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request) -> Response:
"""Create a new study group."""
serializer = StudyGroupSerializer(data=request.data)
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)


class StudyGroupDetailView(APIView):
"""Handle individual study group operations."""

def get(self, request, studygroup_id: str) -> Response:
"""Get all details associated with a given study group."""
try:
study_group = StudyGroup.objects.get(id=studygroup_id)
except StudyGroup.DoesNotExist:
return Response(
{"detail": "Study group not found"}, status=status.HTTP_404_NOT_FOUND
)
serializer = StudyGroupSerializer(study_group)
return Response(serializer.data)

def put(self, request, studygroup_id: str) -> Response:
"""Update an existing study group."""
try:
study_group = StudyGroup.objects.get(id=studygroup_id)
except StudyGroup.DoesNotExist:
return Response(
{"detail": "Study group not found"}, status=status.HTTP_404_NOT_FOUND
)
serializer = StudyGroupSerializer(study_group, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def delete(self, request, studygroup_id: str) -> Response:
"""Delete an existing study group."""
try:
study_group = StudyGroup.objects.get(id=studygroup_id)
except StudyGroup.DoesNotExist:
return Response(
{"detail": "Study group not found"}, status=status.HTTP_404_NOT_FOUND
)
study_group.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
20 changes: 0 additions & 20 deletions backend/hoagiehelp/api/studygroup_views.py

This file was deleted.

Loading