mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 14:20:06 +01:00
feat: paginate categories
This commit is contained in:
@@ -29,19 +29,20 @@ public class CategoryController : ControllerBase
|
||||
|
||||
// GET /api/v1/categories
|
||||
/// <summary>
|
||||
/// Get every category
|
||||
/// Get a page category
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Can (and will) return an empty list if no categories are found in DB. <br/>
|
||||
/// Has CORS set.
|
||||
/// </remarks>
|
||||
/// <param name="page_no">The page number</param>
|
||||
/// <response code="200">Returned on valid request</response>
|
||||
// /// <response code="404">Returned when there are no categories to list</response>
|
||||
[HttpGet]
|
||||
/// <response code="404">Returned when requested page is invalid (page_no <= 0)</response>
|
||||
[HttpGet("{page_no}")]
|
||||
[EnableCors]
|
||||
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
||||
// [ProducesResponseType(typeof(ErrorDTO), 404)]
|
||||
public async Task<IActionResult> GetQuotePage()
|
||||
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
||||
public async Task<IActionResult> GetCategoryPage(int page_no = 1)
|
||||
{
|
||||
// The following seems to be a bad idea, so I leave it as is. ~eee4
|
||||
//
|
||||
@@ -49,12 +50,23 @@ public class CategoryController : ControllerBase
|
||||
//
|
||||
// if (totalCategories <= 0)
|
||||
// {
|
||||
// return NotFound(new ErrorDTO { Status = "error", Error_msg = "No categories to list" });
|
||||
// return NoContent(new ErrorDTO { Status = "error", Error_msg = "No categories to list" });
|
||||
// }
|
||||
|
||||
const int PageSize = 10;
|
||||
|
||||
if (page_no <= 0)
|
||||
{
|
||||
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Numer strony musi być większy niż 0" });
|
||||
}
|
||||
|
||||
// Get all the categories
|
||||
//List<Category> categories = await _db.Categories
|
||||
// .ToListAsync();
|
||||
List<Category> categories = await _db.Categories
|
||||
.ToListAsync();
|
||||
.Skip((page_no - 1) * PageSize)
|
||||
.Take(PageSize)
|
||||
.ToListAsync();
|
||||
|
||||
// Convert them to a list of DTO
|
||||
List<CategoryShortDTO> result = categories
|
||||
|
||||
Reference in New Issue
Block a user