diff --git a/backend/config/urls.py b/backend/config/urls.py index 48bd3f0..6169f85 100644 --- a/backend/config/urls.py +++ b/backend/config/urls.py @@ -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, @@ -52,6 +53,13 @@ path( "comments//", CommentDetailView.as_view(), name="comment-detail" ), + # Study Groups + path("study-groups/", StudyGroupListView.as_view(), name="studygroup-list"), + path( + "study-groups//", + StudyGroupDetailView.as_view(), + name="studygroup-detail", + ), # Users path("users//", UserView.as_view(), name="user-detail"), path("users//questions/", user_questions, name="user-questions"), diff --git a/backend/hoagiehelp/api/study_group_views.py b/backend/hoagiehelp/api/study_group_views.py new file mode 100644 index 0000000..27cb53f --- /dev/null +++ b/backend/hoagiehelp/api/study_group_views.py @@ -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) diff --git a/backend/hoagiehelp/api/studygroup_views.py b/backend/hoagiehelp/api/studygroup_views.py deleted file mode 100644 index 8a6eb39..0000000 --- a/backend/hoagiehelp/api/studygroup_views.py +++ /dev/null @@ -1,20 +0,0 @@ -from rest_framework import serializers - -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", - )