mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 19:00:07 +01:00
chore: documentation for category modification
This commit is contained in:
@@ -155,31 +155,49 @@ public class CategoryController : ControllerBase
|
|||||||
return Ok(new { Status = "ok" });
|
return Ok(new { Status = "ok" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PATCH /api/v1/categories/1
|
||||||
|
/// <summary>
|
||||||
|
/// [AUTHED] Modify an existing category
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Allows authorized users to modify categories.
|
||||||
|
/// <br/><br/>
|
||||||
|
/// Has CORS set.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="id">Id of the category which shall be modified</param>
|
||||||
|
/// <param name="updatedCategory">DTO with new name and description. Id and creation date are ignored.</param>
|
||||||
|
/// <response code="200">Returned on valid request</response>
|
||||||
|
/// <response code="400">Returned when category name is empty or null</response>
|
||||||
|
/// <response code="404">Returned when no such category exists</response>
|
||||||
[HttpPatch("{id}")]
|
[HttpPatch("{id}")]
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[EnableCors]
|
[EnableCors]
|
||||||
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
||||||
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
||||||
public async Task<IActionResult> EditCategory(int id, [FromBody] CategoryShortDTO updatedCategory)
|
public async Task<IActionResult> EditCategory(int id, [FromBody] CategoryShortDTO updatedCategory)
|
||||||
{
|
{
|
||||||
Category? cat= await _db.Categories.FirstOrDefaultAsync(c => c.Id == id);
|
// Find the category to modify
|
||||||
|
Category? cat = await _db.Categories.FirstOrDefaultAsync(c => c.Id == id);
|
||||||
|
|
||||||
|
// Failed?
|
||||||
if (cat == null)
|
if (cat == null)
|
||||||
return NotFound(new { status = "error", error_msg = "Category not found" });
|
return NotFound(new { status = "error", error_msg = "Category not found" });
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(updatedCategory.Name) || string.IsNullOrWhiteSpace(updatedCategory.Description))
|
// Otherwise, ensure the category name is not empty or null
|
||||||
return BadRequest(new ErrorDTO { Status = "error", Error_msg = "Text and author are required." });
|
if (string.IsNullOrWhiteSpace(updatedCategory.Name))
|
||||||
|
return BadRequest(new ErrorDTO { Status = "error", Error_msg = "Category name cannot be empty." });
|
||||||
cat.Name=updatedCategory.Name;
|
|
||||||
cat.Description=updatedCategory.Description;
|
|
||||||
|
|
||||||
await _db.SaveChangesAsync();
|
|
||||||
return Ok(cat.ToCategoryShortDTO());
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Update category
|
// Update the fields
|
||||||
// PATCH /api/v1/categories/1
|
cat.Name = updatedCategory.Name;
|
||||||
|
cat.Description = updatedCategory.Description;
|
||||||
|
// Note the user cannot modify the createdAt field,
|
||||||
|
// and we do not store last modification date.
|
||||||
|
await _db.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Return the modified category to user
|
||||||
|
return Ok(cat.ToCategoryShortDTO());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user