mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 16:00:06 +01:00
losowanie z kategoria
This commit is contained in:
@@ -185,46 +185,60 @@ public class QuotesController : ControllerBase
|
|||||||
[HttpGet("random")]
|
[HttpGet("random")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
||||||
public async Task<IActionResult> GetRandomQuote()
|
[ProducesResponseType(204)]
|
||||||
{
|
public async Task<IActionResult> GetRandomQuote([FromQuery] int? category_id = null)
|
||||||
var totalQuotes = await _db.Quotes.CountAsync();
|
{
|
||||||
if (totalQuotes == 0)
|
IQueryable<Quote> query = _db.Quotes.Include(q => q.QuoteCategories!).ThenInclude(qc => qc.Category);
|
||||||
return NotFound(new ErrorDTO { Status = "error", Error_msg = "No quotes to choose from" });
|
|
||||||
|
if (category_id.HasValue)
|
||||||
var random = new Random();
|
{
|
||||||
var skip = random.Next(0, totalQuotes);
|
query = query.Where(q => q.QuoteCategories!.Any(qc => qc.CategoryId == category_id.Value));
|
||||||
|
}
|
||||||
var quote = await _db.Quotes
|
|
||||||
.Include(q => q.QuoteCategories!)
|
var totalQuotes = await query.CountAsync();
|
||||||
.ThenInclude(qc => qc.Category)
|
if (totalQuotes == 0)
|
||||||
.Skip(skip)
|
{
|
||||||
.Take(1)
|
|
||||||
.FirstOrDefaultAsync();
|
if (category_id.HasValue)
|
||||||
|
return NoContent(); // Brak cytatów w wybranej kategorii
|
||||||
if (quote == null)
|
else
|
||||||
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" });
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "No quotes to choose from" });
|
||||||
|
}
|
||||||
Image? image = null;
|
|
||||||
if (quote.ImageId != 0)
|
var random = new Random();
|
||||||
{
|
var skip = random.Next(0, totalQuotes);
|
||||||
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
|
|
||||||
}
|
var quote = await query
|
||||||
|
.Include(q => q.QuoteCategories!)
|
||||||
var dto = new QuoteShortDTO
|
.ThenInclude(qc => qc.Category)
|
||||||
{
|
.Skip(skip)
|
||||||
Id = quote.Id,
|
.Take(1)
|
||||||
Text = quote.Text,
|
.FirstOrDefaultAsync();
|
||||||
Author = quote.Author,
|
|
||||||
ImageUrl = image?.Url,
|
if (quote == null)
|
||||||
Categories = quote.QuoteCategories?
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" });
|
||||||
.Select(qc => qc.Category?.Name ?? "")
|
|
||||||
.Where(name => !string.IsNullOrEmpty(name))
|
Image? image = null;
|
||||||
.ToList() ?? new List<string>()
|
if (quote.ImageId != 0)
|
||||||
};
|
{
|
||||||
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
|
||||||
return Ok(dto);
|
}
|
||||||
|
|
||||||
|
var dto = new QuoteShortDTO
|
||||||
|
{
|
||||||
|
Id = quote.Id,
|
||||||
|
Text = quote.Text,
|
||||||
|
Author = quote.Author,
|
||||||
|
ImageUrl = image?.Url,
|
||||||
|
Categories = quote.QuoteCategories?
|
||||||
|
.Select(qc => qc.Category?.Name ?? "")
|
||||||
|
.Where(name => !string.IsNullOrEmpty(name))
|
||||||
|
.ToList() ?? new List<string>()
|
||||||
|
};
|
||||||
|
|
||||||
|
return Ok(dto);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DELETE /api/v1/quotes/{id}
|
// DELETE /api/v1/quotes/{id}
|
||||||
|
|||||||
Reference in New Issue
Block a user