diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index c9cca5e..2a85bed 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -41,35 +41,63 @@ public class QuotesController : ControllerBase /// Has CORS set, unlike e.g. GET /api/v1/quote/{id} or GET /api/v1/quote/random. /// /// The page number - /// A page (10 quotes) + /// (Optional) Standalone category id or comma separated ids (e.g. "1" or "1,2,3") + /// A page (<= 10 quotes) /// Returned on valid request /// Returned when requested page is invalid (page_no <= 0) [HttpGet("page/{page_no}")] [EnableCors] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(ErrorDTO), 404)] - public async Task GetQuotePage(int page_no) + public async Task GetQuotePage(int page_no, [FromQuery] string? category_id = null) { var totalQuotes = await _db.Quotes.CountAsync(); const int PageSize = 10; + List? categories = category_id? + .Split(",") + .Select(Int32.Parse) + .ToList(); + 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); + + // Botched solution + List pageQuotes; + + // Filtrowanie przed pobraniem strony + if (categories != null) + { + pageQuotes = await baseQuery + .Where(q => q.QuoteCategories! + .Any(qc => categories.Contains(qc.CategoryId)) + //.Any(qc => qc.CategoryId == category_id.Value) + ) + .Skip((page_no - 1) * PageSize) + .Take(PageSize) + .ToListAsync(); + } + else + { + pageQuotes = await baseQuery + .Skip((page_no - 1) * PageSize) + .Take(PageSize) + .ToListAsync(); + } + + var result = pageQuotes + .Select(q => q.ToQuoteShortDTO()) + .ToList(); return Ok(result); @@ -118,13 +146,13 @@ public class QuotesController : ControllerBase [HttpPost("new")] [Authorize] [EnableCors] - [ProducesResponseType(201)] + [ProducesResponseType(201)] [ProducesResponseType(typeof(ErrorDTO), 400)] [ProducesResponseType(typeof(ErrorDTO), 403)] public async Task CreateQuote([FromBody] CreateQuoteDTO request) { // Get user ID from claims - + var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId)) // https://stackoverflow.com/a/47708867 @@ -182,7 +210,7 @@ public class QuotesController : ControllerBase /// A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message /// Returned on valid request /// Returned when no quotes exist matching provided criteria - /// Returned when no quotes exist in the DB + /// Returned when no quotes exist (in the DB) [HttpGet("random")] [AllowAnonymous] [ProducesResponseType(typeof(QuoteShortDTO), 200)] @@ -252,7 +280,7 @@ public class QuotesController : ControllerBase { // (Attempt to) find the quote Quote? quote = await _db.Quotes - .FirstOrDefaultAsync(q => q.Id == id); + .FirstOrDefaultAsync(q => q.Id == id); // Failed? if (quote == null) return NotFound(new { status = "error", error_msg = "Quote not found" }); @@ -362,7 +390,8 @@ public class QuotesController : ControllerBase foreach (var name in newNames) { // Add them to the DB - var newCat = new Category { + var newCat = new Category + { Name = name, Description = string.Empty, CreatedAt = DateTime.UtcNow @@ -377,7 +406,8 @@ public class QuotesController : ControllerBase // Assign all the new categories to the quote quote.QuoteCategories = categoriesFromDb - .Select(cat => new QuoteCategory { + .Select(cat => new QuoteCategory + { CategoryId = cat.Id, QuoteId = quote.Id })