Skip to content
Open
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
19 changes: 19 additions & 0 deletions backend/src/modules/exam-clone/exam-clone.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ export class ExamCloneController {
return { leaderboard, userRank };
}

// ==================== EXAM HISTORY ====================

@Get('history')
@ApiOperation({ summary: 'Get paginated exam attempt history' })
@ApiResponse({ status: 200, description: 'Exam history with pagination' })
async getExamHistory(
@CurrentUser() user: JwtPayload,
@Query('limit') limit?: string,
@Query('offset') offset?: string,
) {
return this.examCloneService.getExamHistory(
user.sub,
limit ? parseInt(limit, 10) : 10,
offset ? parseInt(offset, 10) : 0,
);
}

// ==================== QUESTIONS ====================

@Post('questions/:questionId/explanation')
@ApiOperation({ summary: 'Get AI explanation for question' })
@ApiResponse({ status: 200, description: 'Explanation returned' })
Expand Down
81 changes: 81 additions & 0 deletions backend/src/modules/exam-clone/exam-clone.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1643,4 +1643,85 @@
totalCorrect: parseInt(result.total_correct, 10),
};
}

// ==================== EXAM HISTORY ====================

/**
* Get paginated exam history for a user
* @param userId - User ID
* @param limit - Number of records to fetch (default: 10)
* @param offset - Number of records to skip (default: 0)
* @returns Paginated exam attempts with total count
*/
async getExamHistory(
userId: string,
limit: number = 10,
offset: number = 0,
): Promise<{
data: Array<{
id: string;
examCloneId: string;
examTitle: string;
score: number;
correctCount: number;
wrongCount: number;
unansweredCount: number;
totalQuestions: number;
timeSpent: number;
createdAt: Date;
}>;
total: number;
limit: number;
offset: number;
}> {
// Ensure limit and offset are positive integers
const normalizedLimit = Math.max(1, Math.min(parseInt(String(limit), 10) || 10, 100));
const normalizedOffset = Math.max(0, parseInt(String(offset), 10) || 0);

// Get total count
const countResult = await this.db.queryOne<{ count: string }>(
'SELECT COUNT(*) as count FROM exam_attempts WHERE user_id = $1',
[userId],
);
const total = parseInt(countResult?.count || '0', 10);

// Get paginated attempts with exam clone info
const attempts = await this.db.queryMany<any>(

Check warning on line 1689 in backend/src/modules/exam-clone/exam-clone.service.ts

View workflow job for this annotation

GitHub Actions / Backend Checks

Unexpected any. Specify a different type
`SELECT
ea.id,
ea.exam_clone_id,
ec.title as exam_title,
ea.score,
ea.correct_count,
ea.wrong_count,
ea.unanswered_count,
ea.total_questions,
ea.time_spent,
ea.created_at
FROM exam_attempts ea
LEFT JOIN exam_clones ec ON ea.exam_clone_id = ec.id
WHERE ea.user_id = $1
ORDER BY ea.created_at DESC
LIMIT $2 OFFSET $3`,
[userId, normalizedLimit, normalizedOffset],
);

return {
data: attempts.map((a) => ({
id: a.id,
examCloneId: a.exam_clone_id,
examTitle: a.exam_title || 'Deleted Exam',
score: parseInt(a.score, 10),
correctCount: parseInt(a.correct_count, 10),
wrongCount: parseInt(a.wrong_count, 10),
unansweredCount: parseInt(a.unanswered_count, 10),
totalQuestions: parseInt(a.total_questions, 10),
timeSpent: parseInt(a.time_spent, 10),
createdAt: new Date(a.created_at),
})),
total,
limit: normalizedLimit,
offset: normalizedOffset,
};
}
}
Loading