chore: documentation and formatting for random quote and deleting categories

This commit is contained in:
2025-07-21 13:27:11 +02:00
parent d502e9d120
commit b96c780533
3 changed files with 88 additions and 94 deletions

View File

@@ -1,5 +1,5 @@
[*] [*]
end_of_line = crlf end_of_line = lf
charset = utf-8 charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true

View File

@@ -110,6 +110,18 @@ public class CategoryController : ControllerBase
} }
// DELETE /api/v1/categories
/// <summary>
/// [AUTHED] Delete a category
/// </summary>
/// <remarks>
/// Allows authorized users to delete categories.
/// <br/><br/>
/// Has CORS set.
/// </remarks>
/// <param name="id">Id of the category which shall be deleted</param>
/// <response code="200">Returned on valid request</response>
/// <response code="404">Returned when no such category exists</response>
[HttpDelete("{id}")] [HttpDelete("{id}")]
[Authorize] [Authorize]
[EnableCors] [EnableCors]
@@ -117,30 +129,28 @@ public class CategoryController : ControllerBase
[ProducesResponseType(typeof(ErrorDTO), 404)] [ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> DeleteCategory(int id) public async Task<IActionResult> DeleteCategory(int id)
{ {
// (Attempt to) find the quote // (Attempt to) find the category
Category? cat = await _db.Categories Category? cat = await _db.Categories
.FirstOrDefaultAsync(c => c.Id == id); .FirstOrDefaultAsync(c => c.Id == id);
// Failed? // Failed?
if (cat == null) if (cat == null)
return NotFound(new { status = "error", error_msg = "Quote not found" }); return NotFound(new { status = "error", error_msg = "Category not found" });
List<QuoteCategory> quoteLinks = await _db.QuoteCategories.Where(qc => qc.CategoryId == id).ToListAsync();
// Find all the QuoteId <-> CategoryId pairs for provided id
List<QuoteCategory> quoteLinks = await _db.QuoteCategories
.Where(qc => qc.CategoryId == id)
.ToListAsync();
// For each of the dependent quotes
foreach (var link in quoteLinks) { foreach (var link in quoteLinks) {
// Remove all the associative pairs
_db.QuoteCategories.Remove(link); _db.QuoteCategories.Remove(link);
} }
// Finally, remove the category
_db.Categories.Remove(cat); _db.Categories.Remove(cat);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync();
// ====================================================================== //
// Important! //
// Is this the best we can do? Won't marking the quote as "hidden" //
// be better than explicitly deleting it? ~eee4 //
// ====================================================================== //
// Return ok // Return ok
return Ok(new { Status = "ok" }); return Ok(new { Status = "ok" });
} }
@@ -148,8 +158,5 @@ public class CategoryController : ControllerBase
// TODO: Update category // TODO: Update category
// PATCH /api/v1/categories/1 // PATCH /api/v1/categories/1
// TODO: Delete category
// DELETE /api/v1/categories/1
} }

View File

@@ -181,25 +181,30 @@ public class QuotesController : ControllerBase
/// </summary> /// </summary>
/// <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="404">Returned when no quotes exist</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>
[HttpGet("random")] [HttpGet("random")]
[AllowAnonymous] [AllowAnonymous]
[ProducesResponseType(typeof(QuoteShortDTO), 200)] [ProducesResponseType(typeof(QuoteShortDTO), 200)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
[ProducesResponseType(204)] [ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> GetRandomQuote([FromQuery] int? category_id = null) public async Task<IActionResult> GetRandomQuote([FromQuery] int? category_id = null)
{ {
IQueryable<Quote> query = _db.Quotes.Include(q => q.QuoteCategories!).ThenInclude(qc => qc.Category); IQueryable<Quote> query = _db.Quotes
.Include(q => q.QuoteCategories!)
.ThenInclude(qc => qc.Category);
if (category_id.HasValue) if (category_id.HasValue)
{ {
query = query.Where(q => q.QuoteCategories!.Any(qc => qc.CategoryId == category_id.Value)); query = query
.Where(q => q.QuoteCategories!
.Any(qc => qc.CategoryId == category_id.Value)
);
} }
var totalQuotes = await query.CountAsync(); var totalQuotes = await query.CountAsync();
if (totalQuotes == 0) if (totalQuotes == 0)
{ {
if (category_id.HasValue) if (category_id.HasValue)
return NoContent(); // Brak cytatów w wybranej kategorii return NoContent(); // Brak cytatów w wybranej kategorii
else else
@@ -219,25 +224,7 @@ public class QuotesController : ControllerBase
if (quote == null) if (quote == null)
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" }); return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" });
Image? image = null; return Ok(quote.ToQuoteShortDTO());
if (quote.ImageId != 0)
{
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
}
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);
} }