diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index 90189a1..4ab53b0 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Authorization.Infrastructure; using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; @@ -7,6 +8,7 @@ using QuotifyBE.Data; using QuotifyBE.DTOs; using QuotifyBE.Entities; using QuotifyBE.Mapping; +using System.Reflection.Metadata.Ecma335; using System.Security.Claims; namespace QuotifyBE.Controllers; @@ -224,7 +226,8 @@ public class QuotesController : ControllerBase [HttpDelete("{id}")] [ProducesResponseType(204)] [ProducesResponseType(typeof(ErrorDTO), 404)] - //[Authorize] + [Authorize] + [EnableCors] public async Task DeleteQuote(int id) { var quote = await _db.Quotes @@ -235,5 +238,85 @@ public class QuotesController : ControllerBase return Ok(); } + [HttpPatch("{id}")] + [Authorize] + [EnableCors] + [ProducesResponseType(typeof(QuoteShortDTO), 200)] + [ProducesResponseType(typeof(ErrorDTO), 400)] + [ProducesResponseType(typeof(ErrorDTO), 404)] + + public async Task EditQuote(int id, [FromBody] QuoteShortDTO updatedQuote) + { + var Quote = await _db.Quotes + .Include(q => q.QuoteCategories) + // include image? + .FirstOrDefaultAsync(q => q.Id == id); + + if (Quote == null) return NotFound(new { status = "error", error_msg = "Quote not found" }); + if (string.IsNullOrWhiteSpace(updatedQuote.Text) || string.IsNullOrWhiteSpace(updatedQuote.Author)) + { + return BadRequest(new ErrorDTO { Status = "error", Error_msg = "Text and author are required." }); + } + + Quote.Text = updatedQuote.Text; + Quote.Author = updatedQuote.Author; + Quote.LastUpdatedAt = DateTime.UtcNow; + + Image? image = null; + if (!string.IsNullOrEmpty(updatedQuote.ImageUrl)) + { + image = await _db.Images.FirstOrDefaultAsync(i => i.Url == updatedQuote.ImageUrl); + if (image == null) + { + image = new Image { Url = updatedQuote.ImageUrl }; + _db.Images.Add(image); + await _db.SaveChangesAsync(); + } + } + Quote.Image = image; + + if (updatedQuote.Categories == null) { } + else if (updatedQuote.Categories.Any()) + { + var categoriesFromDb = await _db.Categories + .Where(c => updatedQuote.Categories.Contains(c.Name)) + .ToListAsync(); + + // Dodaj nowe kategorie, których nie ma w bazie + var existingNames = categoriesFromDb + .Select(c => c.Name); + var newNames = updatedQuote.Categories.Except(existingNames).ToList(); + + foreach (var name in newNames) + { + var newCat = new Category { + Name = name, + Description = string.Empty, + CreatedAt = DateTime.UtcNow + }; + _db.Categories.Add(newCat); + categoriesFromDb.Add(newCat); + } + + if (newNames.Any()) + await _db.SaveChangesAsync(); + + // Przypisz cytatowi nowe kategorie + Quote.QuoteCategories = categoriesFromDb + .Select(cat => new QuoteCategory { + CategoryId = cat.Id, + QuoteId = Quote.Id + }) + .ToList(); + } + else + { + // Jeśli brak kategorii w dto, czyścić przypisane kategorie? + Quote.QuoteCategories.Clear(); + } + + await _db.SaveChangesAsync(); + return Ok(Quote.ToQuoteShortDTO()); + } } diff --git a/DTOs/QuoteShortDTO.cs b/DTOs/QuoteShortDTO.cs index a5b15e1..d6332bf 100644 --- a/DTOs/QuoteShortDTO.cs +++ b/DTOs/QuoteShortDTO.cs @@ -6,7 +6,7 @@ public record class QuoteShortDTO public string Text { get; set; } = string.Empty; public string Author { get; set; } = string.Empty; public string? ImageUrl { get; set; } - public List Categories { get; set; } = new(); + public List? Categories { get; set; } = new(); };