Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/lessons/dto/lesson-response.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export class LessonResponseDto {
this.videoStartTimestamp = lesson.videoStartTimestamp || null;
this.order = lesson.order;
this.courseId = lesson.courseId;
this.hasQuiz = lesson.hasQuiz ?? false;
this.quizId = lesson.quizId ?? null;
this.createdAt = lesson.createdAt;
this.updatedAt = lesson.updatedAt;
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/lessons/lessons.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBearerAuth } from '@nestjs/swagger';
import { LessonsService } from './lessons.service';

import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { PaginationDto } from '../common/dto/pagination.dto';
Expand Down Expand Up @@ -63,6 +62,7 @@ export class LessonsController {
};
}

// NOTE: /course/:courseId must be declared BEFORE /:id to avoid shadowing
@Get('course/:courseId')
@ApiOperation({ summary: 'Get lessons for a specific course' })
@ApiResponse({ status: 200, description: 'Lessons retrieved successfully' })
Expand Down Expand Up @@ -95,7 +95,7 @@ export class LessonsController {
@ApiResponse({ status: 200, description: 'Lesson details retrieved successfully', type: LessonResponseDto })
@ApiResponse({ status: 404, description: 'Lesson not found' })
async findOne(@Param('id') id: string): Promise<LessonResponseDto> {
const lesson = await this.lessonsService.findOne(id);
const lesson = await this.lessonsService.findOneWithQuizFlag(id);
return new LessonResponseDto(lesson);
}

Expand Down
3 changes: 2 additions & 1 deletion backend/src/lessons/lessons.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { PassportModule } from '@nestjs/passport';
import { Course } from '../courses/entities/course.entity';
import { Lesson } from './entities/lesson.entity';
import { Quiz } from '../quizzes/entities/quiz.entity';
import { LessonsController } from './lessons.controller';
import { LessonsService } from './lessons.service';
import { AuthModule } from '../auth/auth.module';

@Module({
imports: [
TypeOrmModule.forFeature([Lesson, Course]),
TypeOrmModule.forFeature([Lesson, Course, Quiz]),
PassportModule,
AuthModule,
],
Expand Down
38 changes: 35 additions & 3 deletions backend/src/lessons/lessons.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Course } from '../courses/entities/course.entity';
import { Lesson } from './entities/lesson.entity';
import { Repository } from 'typeorm';
import { In, Repository } from 'typeorm';
import { PaginationService } from '../common/services/pagination.service';
import { PaginatedResult } from '../common/services/pagination.service';
import { CreateLessonDto } from './dto/create-lesson.dto';
Expand All @@ -15,6 +15,8 @@ export class LessonsService {
private lessonRepository: Repository<Lesson>,
@InjectRepository(Course)
private courseRepository: Repository<Course>,
@InjectRepository(Quiz)
private quizRepository: Repository<Quiz>,
private readonly paginationService: PaginationService,
) {}

Expand Down Expand Up @@ -76,7 +78,7 @@ export class LessonsService {
courseId: string,
page: number,
limit: number,
): Promise<PaginatedResult<Lesson>> {
): Promise<PaginatedResult<Lesson & { hasQuiz: boolean; quizId: string | null }>> {
const course = await this.courseRepository.findOne({
where: { id: courseId },
});
Expand All @@ -85,14 +87,29 @@ export class LessonsService {
throw new NotFoundException(`Course with ID ${courseId} not found`);
}

return this.paginationService.paginate(
const result = await this.paginationService.paginate(
this.lessonRepository,
{ page, limit },
{
where: { courseId },
order: { order: 'ASC', createdAt: 'ASC' },
},
);

const lessonIds = result.data.map((l) => l.id);
const quizzes = lessonIds.length
? await this.quizRepository.find({ where: { lessonId: In(lessonIds) }, select: ['id', 'lessonId'] })
: [];
const quizMap = new Map(quizzes.map((q) => [q.lessonId, q.id]));

return {
...result,
data: result.data.map((l) => ({
...l,
hasQuiz: quizMap.has(l.id),
quizId: quizMap.get(l.id) ?? null,
})),
};
}

async findOne(id: string): Promise<Lesson> {
Expand All @@ -108,6 +125,21 @@ export class LessonsService {
return lesson;
}

async findOneWithQuizFlag(id: string): Promise<Lesson & { hasQuiz: boolean; quizId: string | null }> {
const lesson = await this.lessonRepository.findOne({
where: { id },
relations: ['course'],
});

if (!lesson) {
throw new NotFoundException(`Lesson with ID ${id} not found`);
}

const quiz = await this.quizRepository.findOne({ where: { lessonId: id }, select: ['id'] });

return { ...lesson, hasQuiz: !!quiz, quizId: quiz?.id ?? null };
}

async update(id: string, updateLessonDto: UpdateLessonDto): Promise<Lesson> {
const lesson = await this.lessonRepository.findOne({
where: { id },
Expand Down
Loading