feat - meal record filter based on date

This commit is contained in:
bobbyvish
2024-03-21 13:13:06 +05:30
parent 9ba43e8fd1
commit 64c172ab59
2 changed files with 27 additions and 0 deletions

View File

@@ -25,6 +25,7 @@ urlpatterns = [
path("meal-symptoms/<int:pk>/", views.MealSymptomAPIView.as_view()),
path("meal/", views.MealAPIView.as_view()),
path("meal/date/", views.MealDateAPIView.as_view()),
path("meal/<int:pk>/", views.MealAPIView.as_view()),
path("report/", views.ReportAPIView.as_view()),

View File

@@ -617,6 +617,32 @@ class MealAPIView(APIView):
message=constants.RECORD_DELETED, status=status.HTTP_204_NO_CONTENT
)
class MealDateAPIView(APIView):
authentication_classes = [JWTAuthentication]
permission_classes = [IsAuthenticated]
serializer_class = MealRecordSerializer
model = MealRecord
def get(self, request):
date = request.GET.get("date")
if not date:
return ApiResponse.error(
message=constants.FAILURE, errors="Date parameter is missing"
)
try:
# Convert the date string to a datetime object
date_obj = datetime.strptime(date, "%Y-%m-%d").date()
except ValueError:
return ApiResponse.error(
message=constants.FAILURE, errors="Invalid date format"
)
obj = self.model.objects.filter(date=date_obj)
serializer = self.serializer_class(obj, many=True)
return ApiResponse.success(message=constants.SUCCESS, data=serializer.data)
from collections import defaultdict