mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 23:00:07 +01:00
feat: paginate categories
This commit is contained in:
@@ -29,19 +29,20 @@ public class CategoryController : ControllerBase
|
|||||||
|
|
||||||
// GET /api/v1/categories
|
// GET /api/v1/categories
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get every category
|
/// Get a page category
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <remarks>
|
/// <remarks>
|
||||||
/// Can (and will) return an empty list if no categories are found in DB. <br/>
|
/// Can (and will) return an empty list if no categories are found in DB. <br/>
|
||||||
/// Has CORS set.
|
/// Has CORS set.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
|
/// <param name="page_no">The page number</param>
|
||||||
/// <response code="200">Returned on valid request</response>
|
/// <response code="200">Returned on valid request</response>
|
||||||
// /// <response code="404">Returned when there are no categories to list</response>
|
/// <response code="404">Returned when requested page is invalid (page_no <= 0)</response>
|
||||||
[HttpGet]
|
[HttpGet("{page_no}")]
|
||||||
[EnableCors]
|
[EnableCors]
|
||||||
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
||||||
// [ProducesResponseType(typeof(ErrorDTO), 404)]
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
||||||
public async Task<IActionResult> GetQuotePage()
|
public async Task<IActionResult> GetCategoryPage(int page_no = 1)
|
||||||
{
|
{
|
||||||
// The following seems to be a bad idea, so I leave it as is. ~eee4
|
// 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)
|
// 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
|
// Get all the categories
|
||||||
|
//List<Category> categories = await _db.Categories
|
||||||
|
// .ToListAsync();
|
||||||
List<Category> categories = await _db.Categories
|
List<Category> categories = await _db.Categories
|
||||||
.ToListAsync();
|
.Skip((page_no - 1) * PageSize)
|
||||||
|
.Take(PageSize)
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
// Convert them to a list of DTO
|
// Convert them to a list of DTO
|
||||||
List<CategoryShortDTO> result = categories
|
List<CategoryShortDTO> result = categories
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Authorization.Infrastructure;
|
|
||||||
using Microsoft.AspNetCore.Cors;
|
using Microsoft.AspNetCore.Cors;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.EntityFrameworkCore.Update.Internal;
|
|
||||||
using QuotifyBE.Data;
|
using QuotifyBE.Data;
|
||||||
using QuotifyBE.DTOs;
|
using QuotifyBE.DTOs;
|
||||||
using QuotifyBE.Entities;
|
using QuotifyBE.Entities;
|
||||||
using QuotifyBE.Mapping;
|
using QuotifyBE.Mapping;
|
||||||
using System.Reflection.Metadata.Ecma335;
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
||||||
namespace QuotifyBE.Controllers;
|
namespace QuotifyBE.Controllers;
|
||||||
@@ -50,7 +47,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, string? sort = "desc", [FromQuery] string? category_id = null)
|
public async Task<IActionResult> GetQuotePage(int page_no = 1, 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;
|
||||||
@@ -197,7 +194,10 @@ public class QuotesController : ControllerBase
|
|||||||
if ( !request.ImageUrl.StartsWith("http://")
|
if ( !request.ImageUrl.StartsWith("http://")
|
||||||
&& !request.ImageUrl.StartsWith("https://")
|
&& !request.ImageUrl.StartsWith("https://")
|
||||||
&& !request.ImageUrl.StartsWith("/"))
|
&& !request.ImageUrl.StartsWith("/"))
|
||||||
return StatusCode(406, new ErrorDTO { Status = "error", Error_msg = "Image URLs should point to http/https url or a local resource" });
|
return StatusCode(406, new ErrorDTO {
|
||||||
|
Status = "error",
|
||||||
|
Error_msg = "Image URLs should point to http/https url or a local resource"
|
||||||
|
});
|
||||||
|
|
||||||
image = new Image { Url = request.ImageUrl };
|
image = new Image { Url = request.ImageUrl };
|
||||||
_db.Images.Add(image);
|
_db.Images.Add(image);
|
||||||
@@ -414,7 +414,10 @@ public class QuotesController : ControllerBase
|
|||||||
if ( !updatedQuote.ImageUrl.StartsWith("http://")
|
if ( !updatedQuote.ImageUrl.StartsWith("http://")
|
||||||
&& !updatedQuote.ImageUrl.StartsWith("https://")
|
&& !updatedQuote.ImageUrl.StartsWith("https://")
|
||||||
&& !updatedQuote.ImageUrl.StartsWith("/"))
|
&& !updatedQuote.ImageUrl.StartsWith("/"))
|
||||||
return StatusCode(406, new ErrorDTO { Status = "error", Error_msg = "Image URLs should point to http/https url or a local resource" });
|
return StatusCode(406, new ErrorDTO {
|
||||||
|
Status = "error",
|
||||||
|
Error_msg = "Image URLs should point to http/https url or a local resource"
|
||||||
|
});
|
||||||
|
|
||||||
image = new Image { Url = updatedQuote.ImageUrl };
|
image = new Image { Url = updatedQuote.ImageUrl };
|
||||||
_db.Images.Add(image);
|
_db.Images.Add(image);
|
||||||
|
|||||||
Reference in New Issue
Block a user