From 3a82e4291e14d36ed8805fee91699f16799ff90a Mon Sep 17 00:00:00 2001 From: kuba Date: Tue, 22 Jul 2025 11:45:48 +0200 Subject: [PATCH] edycja kategorii --- Controllers/CategoryController.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Controllers/CategoryController.cs b/Controllers/CategoryController.cs index aab55e8..3cacd7a 100644 --- a/Controllers/CategoryController.cs +++ b/Controllers/CategoryController.cs @@ -155,6 +155,29 @@ public class CategoryController : ControllerBase return Ok(new { Status = "ok" }); } + [HttpPatch("{id}")] + [Authorize] + [EnableCors] + [ProducesResponseType(typeof(QuoteShortDTO), 200)] + [ProducesResponseType(typeof(ErrorDTO), 400)] + [ProducesResponseType(typeof(ErrorDTO), 404)] + public async Task EditCategory(int id, [FromBody] CategoryShortDTO updatedCategory) + { + Category? cat= await _db.Categories.FirstOrDefaultAsync(c => c.Id == id); + + if (cat == null) + return NotFound(new { status = "error", error_msg = "Category not found" }); + + if (string.IsNullOrWhiteSpace(updatedCategory.Name) || string.IsNullOrWhiteSpace(updatedCategory.Description)) + return BadRequest(new ErrorDTO { Status = "error", Error_msg = "Text and author are required." }); + + cat.Name=updatedCategory.Name; + cat.Description=updatedCategory.Description; + + await _db.SaveChangesAsync(); + return Ok(cat.ToCategoryShortDTO()); + } + // TODO: Update category // PATCH /api/v1/categories/1