From 972b42579d9e5b436e3078a21794647acc5a8154 Mon Sep 17 00:00:00 2001 From: huydinhdevopssoftware Date: Thu, 3 Dec 2026 12:51:11 +0700 Subject: [PATCH] feat(mentor-ai): add AI class content generation, ZIP/Excel import, and update learning path & homework services --- .../Services/HomeworkService.cs | 13 ++------ .../Services/MockTestService.cs | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/RikiPath.Application/Services/HomeworkService.cs b/RikiPath.Application/Services/HomeworkService.cs index 8869a67..d15ab24 100644 --- a/RikiPath.Application/Services/HomeworkService.cs +++ b/RikiPath.Application/Services/HomeworkService.cs @@ -518,20 +518,11 @@ await unit.ExecuteInTransactionAsync(async token => return; } - var exerciseIds = assignment.Exercises.Select(item => item.PracticeExerciseId).ToArray(); - var completed = await unit.PracticeExerciseAttempts.Query().CountAsync(attempt => - attempt.HomeworkAssignmentId == assignmentId && attempt.UserId == userId && - attempt.IsCompleted && !attempt.IsDeleted && exerciseIds.Contains(attempt.PracticeExerciseId), token); - if (completed != exerciseIds.Length) - { - response = ApiResponse.Fail("Hãy làm hết các bài luyện tập trước khi nộp.", - HttpStatusCode.Conflict, "HOMEWORK_EXERCISES_INCOMPLETE"); - return; - } var existing = assignment.Submission is { IsDeleted: false } current ? current : null; var hasStoredScan = existing is not null && ScanReference(existing) is not null; + // Attached exercises may be submitted without a separate answer; the Mentor reviews the work. // Online answers (trắc nghiệm) and audio recordings are valid payloads on their own. - if (completed == 0 && string.IsNullOrEmpty(text) && string.IsNullOrEmpty(link) && scan is null && !hasStoredScan + if (assignment.Exercises.Count == 0 && string.IsNullOrEmpty(text) && string.IsNullOrEmpty(link) && scan is null && !hasStoredScan && request.SelectedOptions is not { Length: > 0 } && audio is null && fileCount == 0) { response = ApiResponse.Fail("Hãy nhập nội dung, đường dẫn hoặc đính kèm ảnh/PDF bài làm."); diff --git a/RikiPath.Application/Services/MockTestService.cs b/RikiPath.Application/Services/MockTestService.cs index b1ca161..4f36bbf 100644 --- a/RikiPath.Application/Services/MockTestService.cs +++ b/RikiPath.Application/Services/MockTestService.cs @@ -1135,6 +1135,7 @@ await unitOfWork.ExecuteInTransactionAsync(async transactionCancellationToken => attempt.QuestionSnapshotJson = JsonSerializer.Serialize(existingSnapshot, MockSnapshotJson); } attempt.ModifiedDate = now; + await PersistProjectedAnswersAsync(attemptId, snapshotAnswers, transactionCancellationToken); unitOfWork.MockTestAttempts.Update(attempt); await unitOfWork.SaveChangesAsync(transactionCancellationToken); response = ApiResponse.Success(new AttemptResultResponse @@ -1178,6 +1179,7 @@ await unitOfWork.MockTestSectionResults.AddRangeAsync( attempt.SubmittedAt = expired && deadline.HasValue ? deadline.Value : now; attempt.IsCompleted = true; attempt.TotalScore = evaluated.OverallAccuracyPercent; + await PersistProjectedAnswersAsync(attemptId, snapshotAnswers, transactionCancellationToken); unitOfWork.MockTestAttempts.Update(attempt); // Answers, section results và trạng thái attempt cùng commit trong một transaction. @@ -1408,6 +1410,7 @@ await unitOfWork.ExecuteInTransactionAsync(async ct => attempt.QuestionSnapshotJson = JsonSerializer.Serialize(existingSnap, MockSnapshotJson); } attempt.ModifiedDate = saveUtc; + await PersistProjectedAnswersAsync(attemptId, snapshots, ct); unitOfWork.MockTestAttempts.Update(attempt); await unitOfWork.SaveChangesAsync(ct); response = ApiResponse.Success(true); @@ -1615,6 +1618,7 @@ await unitOfWork.ExecuteInTransactionAsync(async ct => existingSnap.Scoring = evaluated.Scoring; attempt.QuestionSnapshotJson = JsonSerializer.Serialize(existingSnap, MockSnapshotJson); } + await PersistProjectedAnswersAsync(attemptId, answers, ct); unitOfWork.MockTestAttempts.Update(attempt); await unitOfWork.SaveChangesAsync(ct); @@ -3327,6 +3331,7 @@ public async Task> SavePlacementAnswerAs if (validationError is not null) return ApiResponse.Fail(validationError, HttpStatusCode.BadRequest); + await PersistProjectedAnswersAsync(attemptId, snapshots, cancellationToken); await unitOfWork.SaveChangesAsync(cancellationToken); return ApiResponse.Success(new PlacementAnswerAckResponse { @@ -3378,6 +3383,7 @@ public async Task> SavePlacementAnswe saved++; } + await PersistProjectedAnswersAsync(attemptId, snapshots, cancellationToken); await unitOfWork.SaveChangesAsync(cancellationToken); return ApiResponse.Success(new PlacementAnswersSavedResponse { @@ -3457,6 +3463,7 @@ await unitOfWork.ExecuteInTransactionAsync(async ct => attempt.SubmittedAt = DateTime.UtcNow; attempt.IsCompleted = true; attempt.TotalScore = sections.OverallScore; + await PersistProjectedAnswersAsync(attemptId, snapshots, ct); unitOfWork.MockTestAttempts.Update(attempt); await unitOfWork.SaveChangesAsync(ct); @@ -3565,6 +3572,7 @@ private async Task> LoadAttemptAnswersWithLegacyProjectionA .Where(answer => answer.MockTestAttemptId == attemptId) .OrderBy(answer => answer.MockQuestion.MockTestSection!.SortOrder) .ThenBy(answer => answer.MockQuestion.SortOrder) + .AsNoTracking() .ToListAsync(ct); if (answers.Count == 0) @@ -3727,6 +3735,28 @@ private async Task> LoadAttemptAnswersWithLegacyProjectionA return answers; } + // The projected question/test/level graph above is incomplete and must never be tracked for writes. + // Copy only answer fields back to the real tracked rows before SaveChanges. + private async Task PersistProjectedAnswersAsync( + int attemptId, IReadOnlyCollection projectedAnswers, CancellationToken ct) + { + var byId = projectedAnswers.ToDictionary(answer => answer.Id); + var ids = byId.Keys.ToArray(); + var storedAnswers = await unitOfWork.MockTestAnswers.Query() + .Where(answer => answer.MockTestAttemptId == attemptId && ids.Contains(answer.Id)) + .ToListAsync(ct); + if (storedAnswers.Count != projectedAnswers.Count) + throw new InvalidOperationException("Danh sách câu trả lời của lượt làm bài đã thay đổi."); + + foreach (var stored in storedAnswers) + { + var projected = byId[stored.Id]; + stored.SelectedOptionId = projected.SelectedOptionId; + stored.ResponseJson = projected.ResponseJson; + stored.IsCorrect = projected.IsCorrect; + } + } + // Tổng thời gian = tổng TimeLimitMinutes của các đề tham gia attempt; tính từ StartedAt phía server. private static int GetTimeLimitMinutes(IEnumerable answers) {