fix: pass DTO from API, and pass objects instead of raw ints

fixes cyclic import when passing categories
This commit is contained in:
2025-07-21 10:57:38 +02:00
parent 1f9c04e2fc
commit d99755e7af

View File

@@ -158,13 +158,13 @@ public class QuotesController : ControllerBase
// Attach categories // Attach categories
foreach (var categoryId in request.CategoryIds ?? []) foreach (var categoryId in request.CategoryIds ?? [])
{ {
var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId); Category? category = await _db.Categories.FirstOrDefaultAsync(c => c.Id == categoryId);
if (!categoryExists) if (category == null)
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
{ {
CategoryId = categoryId, Category = category,
Quote = quote Quote = quote
}); });
} }
@@ -172,7 +172,7 @@ public class QuotesController : ControllerBase
_db.Quotes.Add(quote); _db.Quotes.Add(quote);
await _db.SaveChangesAsync(); await _db.SaveChangesAsync();
return CreatedAtAction(nameof(GetQuoteById), new { id = quote.Id }, quote); return CreatedAtAction(nameof(GetQuoteById), new { id = quote.Id }, quote.ToQuoteShortDTO());
} }
// GET /api/v1/quotes/random // GET /api/v1/quotes/random