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.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Update.Internal;
using QuotifyBE.Data; using QuotifyBE.Data;
using QuotifyBE.DTOs; using QuotifyBE.DTOs;
using QuotifyBE.Entities; using QuotifyBE.Entities;
@@ -67,18 +68,19 @@ public class QuotesController : ControllerBase
.ToList(); .ToList();
return Ok(result); return Ok(result);
} }
// GET /api/v1/quotes/{id} // GET /api/v1/quotes/{id}
/// <summary> /// <summary>
/// Get specified quote summary /// [AUTH] Get specified quote summary
/// </summary> /// </summary>
/// <param name="id">The quote id in question</param> /// <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> /// <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="200">Returned on valid request</response>
/// <response code="404">Returned when quote id is invalid or simply doesn't exist</response> /// <response code="404">Returned when quote id is invalid or simply doesn't exist</response>
[HttpGet("{id}")] [HttpGet("{id}")]
[Authorize]
[ProducesResponseType(typeof(QuoteShortDTO), 200)] [ProducesResponseType(typeof(QuoteShortDTO), 200)]
[ProducesResponseType(typeof(ErrorDTO), 404)] [ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> GetQuoteById(int id) public async Task<IActionResult> GetQuoteById(int id)
@@ -109,12 +111,13 @@ public class QuotesController : ControllerBase
[HttpPost("new")] [HttpPost("new")]
[Authorize] [Authorize]
[EnableCors] [EnableCors]
[ProducesResponseType(201)] [ProducesResponseType(201)]
[ProducesResponseType(typeof(ErrorDTO), 400)] [ProducesResponseType(typeof(ErrorDTO), 400)]
[ProducesResponseType(typeof(ErrorDTO), 403)] [ProducesResponseType(typeof(ErrorDTO), 403)]
public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request) public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request)
{ {
// Get user ID from claims // Get user ID from claims
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId)) if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId))
// https://stackoverflow.com/a/47708867 // https://stackoverflow.com/a/47708867
@@ -150,7 +153,7 @@ public class QuotesController : ControllerBase
{ {
var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId); var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId);
if (!categoryExists) 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 quote.QuoteCategories.Add(new QuoteCategory
{ {
@@ -193,7 +196,7 @@ public class QuotesController : ControllerBase
.FirstOrDefaultAsync(); .FirstOrDefaultAsync();
if (quote == null) 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; Image? image = null;
if (quote.ImageId != 0) if (quote.ImageId != 0)
@@ -216,5 +219,21 @@ public class QuotesController : ControllerBase
return Ok(dto); return Ok(dto);
} }
[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();
}
} }