From d502e9d1207f51fa8582265ddc2d270bfe3c79e2 Mon Sep 17 00:00:00 2001 From: kuba Date: Mon, 21 Jul 2025 12:40:13 +0200 Subject: [PATCH] usuwanie kategorii (do przetestowania dla cytatow z kategoriami) --- Controllers/CategoryController.cs | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Controllers/CategoryController.cs b/Controllers/CategoryController.cs index 535864a..9770b00 100644 --- a/Controllers/CategoryController.cs +++ b/Controllers/CategoryController.cs @@ -108,6 +108,41 @@ public class CategoryController : ControllerBase // And send back to the user as DTO return Ok(cat.ToCategoryShortDTO()); + } + + [HttpDelete("{id}")] + [Authorize] + [EnableCors] + [ProducesResponseType(200)] + [ProducesResponseType(typeof(ErrorDTO), 404)] + public async Task DeleteCategory(int id) + { + // (Attempt to) find the quote + Category? cat = await _db.Categories + .FirstOrDefaultAsync(c => c.Id == id); + // Failed? + if (cat == null) + return NotFound(new { status = "error", error_msg = "Quote not found" }); + + List quoteLinks = await _db.QuoteCategories.Where(qc => qc.CategoryId == id).ToListAsync(); + + + + foreach (var link in quoteLinks) { + _db.QuoteCategories.Remove(link); + } + _db.Categories.Remove(cat); + + 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(new { Status = "ok" }); } // TODO: Update category @@ -117,3 +152,4 @@ public class CategoryController : ControllerBase // DELETE /api/v1/categories/1 } +