feat - meal record filter based on date
This commit is contained in:
@@ -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()),
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user