This commit is contained in:
2025-07-18 11:12:55 +02:00
parent af233d9ee9
commit 76258bc0eb

View File

@@ -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;
@@ -72,13 +73,14 @@ public class QuotesController : ControllerBase
// GET /api/v1/quotes/{id}
/// <summary>
/// Get specified quote summary
/// [AUTH] Get specified quote summary
/// </summary>
/// <param name="id">The quote id in question</param>
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
/// <response code="200">Returned on valid request</response>
/// <response code="404">Returned when quote id is invalid or simply doesn't exist</response>
[HttpGet("{id}")]
[Authorize]
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> GetQuoteById(int id)
@@ -115,6 +117,7 @@ public class QuotesController : ControllerBase
public async Task<IActionResult> 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)
@@ -217,4 +220,20 @@ public class QuotesController : ControllerBase
}
[HttpDelete("{id}")]
[ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
//[Authorize]
public async Task<IActionResult> 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();
}
}