diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index 0bb5d86..17d2e2b 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -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. /// /// The page number + /// How to sort the results (desc/asc) /// (Optional) Standalone category id or comma separated ids (e.g. "1" or "1,2,3") /// A page (<= 10 quotes) /// Returned on valid request @@ -49,7 +50,7 @@ public class QuotesController : ControllerBase [EnableCors] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(ErrorDTO), 404)] - public async Task GetQuotePage(int page_no, [FromQuery] string? category_id = null) + public async Task GetQuotePage(int page_no, string? sort = "desc", [FromQuery] string? category_id = null) { var totalQuotes = await _db.Quotes.CountAsync(); const int PageSize = 10; @@ -80,8 +81,15 @@ public class QuotesController : ControllerBase .Include(q => q.QuoteCategories!) .ThenInclude(qc => qc.Category) .Include(q => q.User) - .Include(q => q.Image) - .OrderBy(q => q.Id); + .Include(q => q.Image); + + // Sort the results in ascending/descending order by id + IOrderedQueryable? 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 List pageQuotes; @@ -89,7 +97,7 @@ public class QuotesController : ControllerBase // Filtrowanie przed pobraniem strony if (categories != null) { - pageQuotes = await baseQuery + pageQuotes = await orderedQuery .Where(q => q.QuoteCategories! .Any(qc => categories.Contains(qc.CategoryId)) //.Any(qc => qc.CategoryId == category_id.Value) @@ -100,7 +108,7 @@ public class QuotesController : ControllerBase } else { - pageQuotes = await baseQuery + pageQuotes = await orderedQuery .Skip((page_no - 1) * PageSize) .Take(PageSize) .ToListAsync();