mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 19:00:07 +01:00
Merge remote-tracking branch 'origin/Tydz3,-losowanie-z-kategoria' into Tydz3,-losowanie-z-kategoria
This commit is contained in:
@@ -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.
|
/// Has CORS set, unlike e.g. GET /api/v1/quote/{id} or GET /api/v1/quote/random.
|
||||||
/// </remarks>
|
/// </remarks>
|
||||||
/// <param name="page_no">The page number</param>
|
/// <param name="page_no">The page number</param>
|
||||||
/// <returns>A page (10 quotes)</returns>
|
/// <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>
|
/// <response code="200">Returned on valid request</response>
|
||||||
/// <response code="404">Returned when requested page is invalid (page_no <= 0)</response>
|
/// <response code="404">Returned when requested page is invalid (page_no <= 0)</response>
|
||||||
[HttpGet("page/{page_no}")]
|
[HttpGet("page/{page_no}")]
|
||||||
[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)
|
public async Task<IActionResult> GetQuotePage(int page_no, [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;
|
||||||
|
|
||||||
|
List<int>? categories = category_id?
|
||||||
|
.Split(",")
|
||||||
|
.Select(Int32.Parse)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
if (page_no <= 0)
|
if (page_no <= 0)
|
||||||
{
|
{
|
||||||
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Numer strony musi być większy niż 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
|
// Paginacja bez filtra
|
||||||
.Select(q => q.ToQuoteShortDTO())
|
var baseQuery = _db.Quotes
|
||||||
.ToList();
|
.Include(q => q.QuoteCategories!)
|
||||||
|
.ThenInclude(qc => qc.Category)
|
||||||
|
.Include(q => q.User)
|
||||||
|
.Include(q => q.Image)
|
||||||
|
.OrderBy(q => q.Id);
|
||||||
|
|
||||||
|
// Botched solution
|
||||||
|
List<Quote> 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);
|
return Ok(result);
|
||||||
|
|
||||||
@@ -118,13 +146,13 @@ public class QuotesController : ControllerBase
|
|||||||
[HttpPost("new")]
|
[HttpPost("new")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[EnableCors]
|
[EnableCors]
|
||||||
[ProducesResponseType(201)]
|
[ProducesResponseType(201)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 403)]
|
[ProducesResponseType(typeof(ErrorDTO), 403)]
|
||||||
public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request)
|
public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request)
|
||||||
{
|
{
|
||||||
// Get user ID from claims
|
// Get user ID from claims
|
||||||
|
|
||||||
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
||||||
if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId))
|
if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId))
|
||||||
// https://stackoverflow.com/a/47708867
|
// https://stackoverflow.com/a/47708867
|
||||||
@@ -182,7 +210,7 @@ public class QuotesController : ControllerBase
|
|||||||
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
||||||
/// <response code="200">Returned on valid request</response>
|
/// <response code="200">Returned on valid request</response>
|
||||||
/// <response code="204">Returned when no quotes exist matching provided criteria</response>
|
/// <response code="204">Returned when no quotes exist matching provided criteria</response>
|
||||||
/// <response code="404">Returned when no quotes exist in the DB</response>
|
/// <response code="404">Returned when no quotes exist (in the DB)</response>
|
||||||
[HttpGet("random")]
|
[HttpGet("random")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
||||||
@@ -252,7 +280,7 @@ public class QuotesController : ControllerBase
|
|||||||
{
|
{
|
||||||
// (Attempt to) find the quote
|
// (Attempt to) find the quote
|
||||||
Quote? quote = await _db.Quotes
|
Quote? quote = await _db.Quotes
|
||||||
.FirstOrDefaultAsync(q => q.Id == id);
|
.FirstOrDefaultAsync(q => q.Id == id);
|
||||||
// Failed?
|
// Failed?
|
||||||
if (quote == null)
|
if (quote == null)
|
||||||
return NotFound(new { status = "error", error_msg = "Quote not found" });
|
return NotFound(new { status = "error", error_msg = "Quote not found" });
|
||||||
@@ -362,7 +390,8 @@ public class QuotesController : ControllerBase
|
|||||||
foreach (var name in newNames)
|
foreach (var name in newNames)
|
||||||
{
|
{
|
||||||
// Add them to the DB
|
// Add them to the DB
|
||||||
var newCat = new Category {
|
var newCat = new Category
|
||||||
|
{
|
||||||
Name = name,
|
Name = name,
|
||||||
Description = string.Empty,
|
Description = string.Empty,
|
||||||
CreatedAt = DateTime.UtcNow
|
CreatedAt = DateTime.UtcNow
|
||||||
@@ -377,7 +406,8 @@ public class QuotesController : ControllerBase
|
|||||||
|
|
||||||
// Assign all the new categories to the quote
|
// Assign all the new categories to the quote
|
||||||
quote.QuoteCategories = categoriesFromDb
|
quote.QuoteCategories = categoriesFromDb
|
||||||
.Select(cat => new QuoteCategory {
|
.Select(cat => new QuoteCategory
|
||||||
|
{
|
||||||
CategoryId = cat.Id,
|
CategoryId = cat.Id,
|
||||||
QuoteId = quote.Id
|
QuoteId = quote.Id
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user