diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index acb242c..90189a1 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Update.Internal; using QuotifyBE.Data; using QuotifyBE.DTOs; using QuotifyBE.Entities; @@ -67,18 +68,19 @@ public class QuotesController : ControllerBase .ToList(); return Ok(result); - + } // GET /api/v1/quotes/{id} /// - /// Get specified quote summary + /// [AUTH] Get specified quote summary /// /// The quote id in question /// A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message /// Returned on valid request /// Returned when quote id is invalid or simply doesn't exist [HttpGet("{id}")] + [Authorize] [ProducesResponseType(typeof(QuoteShortDTO), 200)] [ProducesResponseType(typeof(ErrorDTO), 404)] public async Task GetQuoteById(int id) @@ -109,12 +111,13 @@ public class QuotesController : ControllerBase [HttpPost("new")] [Authorize] [EnableCors] - [ProducesResponseType(201)] + [ProducesResponseType(201)] [ProducesResponseType(typeof(ErrorDTO), 400)] [ProducesResponseType(typeof(ErrorDTO), 403)] public async Task CreateQuote([FromBody] CreateQuoteDTO request) { // Get user ID from claims + var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId)) // https://stackoverflow.com/a/47708867 @@ -150,7 +153,7 @@ public class QuotesController : ControllerBase { var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId); if (!categoryExists) - return BadRequest(new ErrorDTO { Status = "error", Error_msg = $"Category ID {categoryId} not found"}); + return BadRequest(new ErrorDTO { Status = "error", Error_msg = $"Category ID {categoryId} not found" }); quote.QuoteCategories.Add(new QuoteCategory { @@ -193,7 +196,7 @@ public class QuotesController : ControllerBase .FirstOrDefaultAsync(); 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; if (quote.ImageId != 0) @@ -216,5 +219,21 @@ public class QuotesController : ControllerBase return Ok(dto); } + + + [HttpDelete("{id}")] + [ProducesResponseType(204)] + [ProducesResponseType(typeof(ErrorDTO), 404)] + //[Authorize] + public async Task DeleteQuote(int id) + { + var quote = await _db.Quotes + .FirstOrDefaultAsync(q => q.Id == id); + if(quote==null) return NotFound(new { status = "error", error_msg = "Quote not found" }); + _db.Quotes.Remove(quote); + await _db.SaveChangesAsync(); + return Ok(); + } + }