paginacja z kategoriami

This commit is contained in:
2025-07-21 13:25:22 +02:00
parent d502e9d120
commit f773f886b4

View File

@@ -48,33 +48,42 @@ public class QuotesController : ControllerBase
[EnableCors]
[ProducesResponseType(typeof(List<QuoteShortDTO>), 200)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> GetQuotePage(int page_no)
public async Task<IActionResult> GetQuotePage(int page_no, [FromQuery] int? category_id = null)
{
var totalQuotes = await _db.Quotes.CountAsync();
const int PageSize = 10;
if (page_no <= 0)
{
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Numer strony musi być większy niż 0" });
}
var quotes = await _db.Quotes
.Include(q => q.QuoteCategories)
.ThenInclude(qc => qc.Category)
.Include(q => q.User)
.Include(q => q.Image)
.OrderBy(q => q.Id)
.Skip((page_no - 1) * PageSize)
.Take(PageSize)
.ToListAsync();
var result = quotes
.Select(q => q.ToQuoteShortDTO())
.ToList();
// Paginacja bez filtra
var baseQuery = _db.Quotes
.Include(q => q.QuoteCategories!)
.ThenInclude(qc => qc.Category)
.Include(q => q.User)
.Include(q => q.Image)
.OrderBy(q => q.Id);
var pageQuotes = await baseQuery
.Skip((page_no - 1) * PageSize)
.Take(PageSize)
.ToListAsync();
// Filtrowanie dopiero po pobraniu strony
if (category_id.HasValue)
{
pageQuotes = pageQuotes
.Where(q => q.QuoteCategories!.Any(qc => qc.CategoryId == category_id.Value))
.ToList();
}
var result = pageQuotes
.Select(q => q.ToQuoteShortDTO())
.ToList();
return Ok(result);
}
// GET /api/v1/quotes/{id}
/// <summary>
/// [AUTHED] Get specified quote summary