dramaling-vocab-learning/backend/DramaLing.Api/Repositories
鄭沛軒 9bebe78740 feat: 完整實現 Azure Speech Services 例句口說練習功能
🎤 Azure Speech Services 整合:
- 安裝 Microsoft.CognitiveServices.Speech v1.38.0
- 實現 IPronunciationAssessmentService 和 AzurePronunciationAssessmentService
- 創建 SpeechController API 端點 (/api/speech/pronunciation-assessment)
- 更新 PronunciationAssessment 資料庫實體和 Migration
- 完整的多維度評分系統 (準確度/流暢度/完整度/韻律)

🖥️ 前端例句口說練習:
- 實現 AudioRecorder 共用組件 (Web Audio API 錄音)
- 創建 speechAssessmentService.ts API 客戶端
- 完整的 SentenceSpeakingQuiz 組件含錄音/評分/結果顯示
- 擴展複習系統支援第3種題目類型 (sentence-speaking)

🔧 系統修復和優化:
- 修復 FlashcardReviewRepository Include 關聯查詢問題
- 修復 ReviewService 圖片 URL 處理邏輯
- 更新 appsettings.json Azure Speech 配置
- 修復 Swagger 文檔生成問題
- 完善依賴注入和服務註冊

📱 用戶體驗:
- 響應式錄音 UI 含進度條和計時
- 智能評分結果展示和改善建議
- 完整的錯誤處理和狀態管理
- 圖片輔助的語境理解

現在 DramaLing 具備完整的 AI 驅動三合一學習系統:
翻卡記憶 → 詞彙選擇 → 例句口說練習 🎉

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-09 02:45:11 +08:00
..
BaseRepository.cs refactor: 移除冗餘接口文件,簡化架構並重新組織測試結構 2025-10-07 23:45:25 +08:00
FlashcardRepository.cs refactor: 移除冗餘接口文件,簡化架構並重新組織測試結構 2025-10-07 23:45:25 +08:00
FlashcardReviewRepository.cs feat: 完整實現 Azure Speech Services 例句口說練習功能 2025-10-09 02:45:11 +08:00
README.md feat: 完成後端架構全面優化 - 階段一二 2025-09-30 03:32:51 +08:00
UserRepository.cs refactor: 移除冗餘接口文件,簡化架構並重新組織測試結構 2025-10-07 23:45:25 +08:00

README.md

Repository 層架構說明

概述

Repository 層負責數據訪問邏輯,提供統一的數據操作介面,遵循 Repository Pattern 和 Unit of Work 模式。

架構結構

Repositories/
├── README.md                 # 本文檔
├── IRepository.cs           # 通用 Repository 介面
├── BaseRepository.cs        # Repository 基礎實作
├── IUserRepository.cs       # 用戶 Repository 介面
└── UserRepository.cs        # 用戶 Repository 實作

核心介面

IRepository - 通用 Repository 介面

提供基礎 CRUD 操作:

  • GetByIdAsync(int id) - 根據 ID 獲取實體
  • GetAllAsync() - 獲取所有實體
  • AddAsync(T entity) - 新增實體
  • UpdateAsync(T entity) - 更新實體
  • DeleteAsync(int id) - 刪除實體

BaseRepository - Repository 基礎實作

實作通用 Repository 介面,提供:

  • Entity Framework Core 集成
  • 統一錯誤處理
  • 異步操作支持
  • 基礎查詢優化

具體 Repository

IUserRepository / UserRepository

專門處理用戶相關數據操作:

  • 繼承自 IRepository<User>
  • 提供用戶特定的查詢方法
  • 處理用戶認證相關邏輯

使用方式

依賴注入配置

ServiceCollectionExtensions.cs 中註冊:

services.AddScoped(typeof(IRepository<>), typeof(BaseRepository<>));
services.AddScoped<IUserRepository, UserRepository>();

在 Service 中使用

public class SomeService
{
    private readonly IUserRepository _userRepository;

    public SomeService(IUserRepository userRepository)
    {
        _userRepository = userRepository;
    }

    public async Task<User> GetUserAsync(int id)
    {
        return await _userRepository.GetByIdAsync(id);
    }
}

設計原則

  1. 單一職責:每個 Repository 只負責一個 Entity 的數據訪問
  2. 介面隔離:提供清晰的介面定義,方便測試和替換實作
  3. 依賴反轉Service 層依賴於 Repository 介面,而非具體實作
  4. 異步優先:所有數據操作都使用異步方法

擴展指南

新增 Repository

  1. 建立介面檔案:I{Entity}Repository.cs
  2. 建立實作檔案:{Entity}Repository.cs
  3. 在 ServiceCollectionExtensions 中註冊
  4. 更新本文檔

實作範例

// IFlashcardRepository.cs
public interface IFlashcardRepository : IRepository<Flashcard>
{
    Task<IEnumerable<Flashcard>> GetByUserIdAsync(int userId);
}

// FlashcardRepository.cs
public class FlashcardRepository : BaseRepository<Flashcard>, IFlashcardRepository
{
    public FlashcardRepository(DramaLingDbContext context) : base(context) { }

    public async Task<IEnumerable<Flashcard>> GetByUserIdAsync(int userId)
    {
        return await _context.Flashcards
            .Where(f => f.UserId == userId)
            .ToListAsync();
    }
}

版本: 1.0 最後更新: 2025-09-30 維護者: DramaLing 開發團隊