feat: sort the quotes from newest first by default

This commit is contained in:
2025-07-22 12:43:35 +02:00
parent 10d2a35e61
commit 9e1e9c86d3

View File

@@ -41,6 +41,7 @@ public class QuotesController : ControllerBase
/// Has CORS set, unlike e.g. GET /api/v1/quote/{id} or GET /api/v1/quote/random. /// Has CORS set, unlike e.g. GET /api/v1/quote/{id} or GET /api/v1/quote/random.
/// </remarks> /// </remarks>
/// <param name="page_no">The page number</param> /// <param name="page_no">The page number</param>
/// <param name="sort">How to sort the results (desc/asc)</param>
/// <param name="category_id">(Optional) Standalone category id or comma separated ids (e.g. "1" or "1,2,3")</param> /// <param name="category_id">(Optional) Standalone category id or comma separated ids (e.g. "1" or "1,2,3")</param>
/// <returns>A page (&lt;= 10 quotes)</returns> /// <returns>A page (&lt;= 10 quotes)</returns>
/// <response code="200">Returned on valid request</response> /// <response code="200">Returned on valid request</response>
@@ -49,7 +50,7 @@ public class QuotesController : ControllerBase
[EnableCors] [EnableCors]
[ProducesResponseType(typeof(List<QuoteShortDTO>), 200)] [ProducesResponseType(typeof(List<QuoteShortDTO>), 200)]
[ProducesResponseType(typeof(ErrorDTO), 404)] [ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> GetQuotePage(int page_no, [FromQuery] string? category_id = null) public async Task<IActionResult> GetQuotePage(int page_no, string? sort = "desc", [FromQuery] string? category_id = null)
{ {
var totalQuotes = await _db.Quotes.CountAsync(); var totalQuotes = await _db.Quotes.CountAsync();
const int PageSize = 10; const int PageSize = 10;
@@ -80,8 +81,15 @@ public class QuotesController : ControllerBase
.Include(q => q.QuoteCategories!) .Include(q => q.QuoteCategories!)
.ThenInclude(qc => qc.Category) .ThenInclude(qc => qc.Category)
.Include(q => q.User) .Include(q => q.User)
.Include(q => q.Image) .Include(q => q.Image);
.OrderBy(q => q.Id);
// Sort the results in ascending/descending order by id
IOrderedQueryable<Quote>? orderedQuery;
if (sort != null && sort.Equals("asc"))
orderedQuery = baseQuery.OrderBy(q => q.Id);
else
// Sort in descending order by default
orderedQuery = baseQuery.OrderByDescending(q => q.Id);
// Botched solution // Botched solution
List<Quote> pageQuotes; List<Quote> pageQuotes;
@@ -89,7 +97,7 @@ public class QuotesController : ControllerBase
// Filtrowanie przed pobraniem strony // Filtrowanie przed pobraniem strony
if (categories != null) if (categories != null)
{ {
pageQuotes = await baseQuery pageQuotes = await orderedQuery
.Where(q => q.QuoteCategories! .Where(q => q.QuoteCategories!
.Any(qc => categories.Contains(qc.CategoryId)) .Any(qc => categories.Contains(qc.CategoryId))
//.Any(qc => qc.CategoryId == category_id.Value) //.Any(qc => qc.CategoryId == category_id.Value)
@@ -100,7 +108,7 @@ public class QuotesController : ControllerBase
} }
else else
{ {
pageQuotes = await baseQuery pageQuotes = await orderedQuery
.Skip((page_no - 1) * PageSize) .Skip((page_no - 1) * PageSize)
.Take(PageSize) .Take(PageSize)
.ToListAsync(); .ToListAsync();