mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 15:40:07 +01:00
feat: sort the quotes from newest first by default
This commit is contained in:
@@ -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.
|
||||
/// </remarks>
|
||||
/// <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>
|
||||
/// <returns>A page (<= 10 quotes)</returns>
|
||||
/// <response code="200">Returned on valid request</response>
|
||||
@@ -49,7 +50,7 @@ public class QuotesController : ControllerBase
|
||||
[EnableCors]
|
||||
[ProducesResponseType(typeof(List<QuoteShortDTO>), 200)]
|
||||
[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();
|
||||
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<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
|
||||
List<Quote> 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();
|
||||
|
||||
Reference in New Issue
Block a user