dramaling-vocab-learning/backend/DramaLing.Api/Repositories
鄭沛軒 e8ab42dfd7 feat: 實現 /api/flashcards/due API 完整功能
 新增功能
- 建立 FlashcardReview 實體與資料庫表格
- 實現間隔重複算法 (2^n 天公式)
- 支援信心度評估系統 (0=答錯, 1-2=答對)
- 完整的複習統計與進度追蹤

🔧 技術實作
- FlashcardReviewRepository: 優化查詢避免 SQLite APPLY 限制
- ReviewService: 業務邏輯與算法實現
- FlashcardsController: 新增 GET /due 和 POST /{id}/review 端點
- 資料庫遷移與索引優化

📊 API 功能
- 支援查詢參數: limit, includeToday, includeOverdue, favoritesOnly
- 返回格式完全兼容前端 api_seeds.json 結構
- 包含完整 reviewInfo 複習狀態信息
- API 已測試確認在 http://localhost:5000 正常運作

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-06 20:49:40 +08:00
..
BaseRepository.cs feat: 實施全面的程式碼架構優化 2025-09-23 19:00:17 +08:00
FlashcardRepository.cs feat: 修復圖片生成服務 + 統一播放按鈕設計 + API 完善 2025-10-02 03:58:03 +08:00
FlashcardReviewRepository.cs feat: 實現 /api/flashcards/due API 完整功能 2025-10-06 20:49:40 +08:00
IFlashcardRepository.cs feat: 完成後端架構全面優化 - 階段一二 2025-09-30 03:32:51 +08:00
IFlashcardReviewRepository.cs feat: 實現 /api/flashcards/due API 完整功能 2025-10-06 20:49:40 +08:00
IRepository.cs feat: 實施全面的程式碼架構優化 2025-09-23 19:00:17 +08:00
IUserRepository.cs feat: 實施全面的程式碼架構優化 2025-09-23 19:00:17 +08:00
README.md feat: 完成後端架構全面優化 - 階段一二 2025-09-30 03:32:51 +08:00
UserRepository.cs feat: 啟用智能快取系統,實現 57,200 倍性能提升 2025-09-23 19:50:53 +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 開發團隊