98 lines
3.2 KiB
C#
98 lines
3.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using DramaLing.Api.Data;
|
|
using DramaLing.Api.Models.Entities;
|
|
|
|
namespace DramaLing.Api.Repositories;
|
|
|
|
public class FlashcardRepository : BaseRepository<Flashcard>, IFlashcardRepository
|
|
{
|
|
public FlashcardRepository(DramaLingDbContext context, ILogger<BaseRepository<Flashcard>> logger) : base(context, logger) { }
|
|
|
|
public async Task<IEnumerable<Flashcard>> GetByUserIdAsync(Guid userId, string? search = null, bool favoritesOnly = false)
|
|
{
|
|
var query = _context.Flashcards
|
|
.Where(f => f.UserId == userId && !f.IsArchived)
|
|
.AsQueryable();
|
|
|
|
// 搜尋篩選
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(f =>
|
|
f.Word.Contains(search) ||
|
|
f.Translation.Contains(search) ||
|
|
(f.Definition != null && f.Definition.Contains(search)));
|
|
}
|
|
|
|
// 收藏篩選
|
|
if (favoritesOnly)
|
|
{
|
|
query = query.Where(f => f.IsFavorite);
|
|
}
|
|
|
|
return await query
|
|
.Include(f => f.FlashcardExampleImages)
|
|
.ThenInclude(fei => fei.ExampleImage)
|
|
.OrderByDescending(f => f.CreatedAt)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Flashcard?> GetByUserIdAndFlashcardIdAsync(Guid userId, Guid flashcardId)
|
|
{
|
|
return await _context.Flashcards
|
|
.Include(f => f.FlashcardExampleImages)
|
|
.ThenInclude(fei => fei.ExampleImage)
|
|
.FirstOrDefaultAsync(f => f.Id == flashcardId && f.UserId == userId && !f.IsArchived);
|
|
}
|
|
|
|
public async Task<int> GetCountByUserIdAsync(Guid userId, string? search = null, bool favoritesOnly = false)
|
|
{
|
|
var query = _context.Flashcards
|
|
.Where(f => f.UserId == userId && !f.IsArchived)
|
|
.AsQueryable();
|
|
|
|
// 搜尋篩選
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(f =>
|
|
f.Word.Contains(search) ||
|
|
f.Translation.Contains(search) ||
|
|
(f.Definition != null && f.Definition.Contains(search)));
|
|
}
|
|
|
|
// 收藏篩選
|
|
if (favoritesOnly)
|
|
{
|
|
query = query.Where(f => f.IsFavorite);
|
|
}
|
|
|
|
return await query.CountAsync();
|
|
}
|
|
|
|
public async Task<IEnumerable<Flashcard>> GetPagedByUserIdAsync(Guid userId, int page, int pageSize, string? search = null, bool favoritesOnly = false)
|
|
{
|
|
var query = _context.Flashcards
|
|
.Where(f => f.UserId == userId && !f.IsArchived)
|
|
.AsQueryable();
|
|
|
|
// 搜尋篩選
|
|
if (!string.IsNullOrEmpty(search))
|
|
{
|
|
query = query.Where(f =>
|
|
f.Word.Contains(search) ||
|
|
f.Translation.Contains(search) ||
|
|
(f.Definition != null && f.Definition.Contains(search)));
|
|
}
|
|
|
|
// 收藏篩選
|
|
if (favoritesOnly)
|
|
{
|
|
query = query.Where(f => f.IsFavorite);
|
|
}
|
|
|
|
return await query
|
|
.OrderByDescending(f => f.CreatedAt)
|
|
.Skip((page - 1) * pageSize)
|
|
.Take(pageSize)
|
|
.ToListAsync();
|
|
}
|
|
} |