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