diff --git a/apps/course/urls.py b/apps/course/urls.py index 3af3994..e08572d 100644 --- a/apps/course/urls.py +++ b/apps/course/urls.py @@ -13,6 +13,8 @@ urlpatterns = [ path('/attachments/', views.AttachmentListAPIView.as_view(), name='course-attachment-list'), path('/glossaries/', views.GlossaryListAPIView.as_view(), name='course-glossary-list'), path('/lessons/', views.LessonListView.as_view(), name='course-lesson-list'), + path('lesson//', views.LessonDetailView.as_view(), name='lesson-detail'), + path('/participants/', views.CourseParticipantsView.as_view(), name='course-participant-list'), # path('/participant/join/', views.ParticipantCreateView.as_view(), name='course-participant-join'), diff --git a/apps/course/views/lesson.py b/apps/course/views/lesson.py index cec6a12..bf87a3d 100644 --- a/apps/course/views/lesson.py +++ b/apps/course/views/lesson.py @@ -4,6 +4,7 @@ from drf_yasg.utils import swagger_auto_schema from drf_yasg import openapi from django.shortcuts import get_object_or_404 from rest_framework import status +from rest_framework.response import Response from apps.course.serializers import ( LessonSerializer @@ -32,3 +33,40 @@ class LessonListView(ListAPIView): raise AppAPIException({"message": "course not found"}, status_code=status.HTTP_404_NOT_FOUND) return self.queryset.filter(course=course).order_by('priority','id') + + + + +class LessonDetailView(RetrieveAPIView): + serializer_class = LessonSerializer + + def get(self, request, *args, **kwargs): + lesson_id = self.kwargs.get('id') + lesson = get_object_or_404(Lesson, id=lesson_id, is_active=True) + + # Get the next and previous lessons based on priority and id + next_lesson = Lesson.objects.filter( + course=lesson.course, + is_active=True, + priority__gte=lesson.priority, + id__gt=lesson.id + ).order_by('priority', 'id').first() + + previous_lesson = Lesson.objects.filter( + course=lesson.course, + is_active=True, + priority__lte=lesson.priority, + id__lt=lesson.id + ).order_by('-priority', '-id').first() + + # Serialize the current lesson + lesson_data = self.get_serializer(lesson).data + + # Add next and previous lesson ids + lesson_data['next_lesson_id'] = next_lesson.id if next_lesson else None + lesson_data['previous_lesson_id'] = previous_lesson.id if previous_lesson else None + + lesson_data['can_go_next'] = next_lesson is not None + + return Response(lesson_data) +