Fakultas Ilmu Komputer UI

Added weekly_report_id parameter in get weekly report comment API

parent 2c2939e6
......@@ -406,6 +406,20 @@ class WeeklyReportCommentTest(APITestCase):
self.assertEqual(response.data, serializer.data)
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_weekly_report_comment_by_weekly_report_id_succeed(self):
response = self.client.get(f"{self.BASE_URL}?weekly_report_id={self.weekly_report_comment.weekly_report.id}")
weekly_report = WeeklyReport.objects.get(id=self.weekly_report_comment.weekly_report.id)
weekly_report_comments = WeeklyReportComment.objects.get(weekly_report=weekly_report)
serializer = WeeklyReportCommentSerializer(weekly_report_comments)
self.assertEqual(response.data, [serializer.data])
self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_get_weekly_report_comment_by_weekly_report_id_failed(self):
response = self.client.get(f"{self.BASE_URL}?weekly_report_id=-1")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_single_weekly_report_comment_succeed(self):
url = f"{self.BASE_URL}{self.weekly_report_comment.pk}/"
response = self.client.get(url)
......
......@@ -3,7 +3,8 @@ from .views import WeeklyReportViewSet, WeeklyReportCommentViewSet
router = routers.SimpleRouter()
router.register('progress/user_report', WeeklyReportViewSet)
router.register('progress/nutritionist_comment', WeeklyReportCommentViewSet)
router.register('progress/nutritionist_comment', WeeklyReportCommentViewSet, \
basename='WeeklyReportComment')
urlpatterns = []
......
......@@ -52,4 +52,25 @@ class WeeklyReportViewSet(viewsets.ModelViewSet):
class WeeklyReportCommentViewSet(viewsets.ModelViewSet):
serializer_class = WeeklyReportCommentSerializer
queryset = WeeklyReportComment.objects.all()
def get_queryset(self):
queryset = WeeklyReportComment.objects.all()
weekly_report_id = self.request.query_params.get('weekly_report_id')
if weekly_report_id is not None:
try:
weekly_report = WeeklyReport.objects.get(pk=weekly_report_id)
except WeeklyReport.DoesNotExist:
return None
queryset = queryset.filter(Q(weekly_report=weekly_report))
return queryset
def list(self, request):
queryset = self.get_queryset()
if queryset is None:
return Response({'message': 'Weekly Report does not exist.'}, \
status=status.HTTP_400_BAD_REQUEST)
serializer = WeeklyReportCommentSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment