edycja naprawiona

This commit is contained in:
2025-07-18 12:12:22 +02:00
parent 908a56665d
commit 7d20e4d4f9
2 changed files with 85 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authorization.Infrastructure;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
@@ -7,6 +8,7 @@ using QuotifyBE.Data;
using QuotifyBE.DTOs; using QuotifyBE.DTOs;
using QuotifyBE.Entities; using QuotifyBE.Entities;
using QuotifyBE.Mapping; using QuotifyBE.Mapping;
using System.Reflection.Metadata.Ecma335;
using System.Security.Claims; using System.Security.Claims;
namespace QuotifyBE.Controllers; namespace QuotifyBE.Controllers;
@@ -224,7 +226,8 @@ public class QuotesController : ControllerBase
[HttpDelete("{id}")] [HttpDelete("{id}")]
[ProducesResponseType(204)] [ProducesResponseType(204)]
[ProducesResponseType(typeof(ErrorDTO), 404)] [ProducesResponseType(typeof(ErrorDTO), 404)]
//[Authorize] [Authorize]
[EnableCors]
public async Task<IActionResult> DeleteQuote(int id) public async Task<IActionResult> DeleteQuote(int id)
{ {
var quote = await _db.Quotes var quote = await _db.Quotes
@@ -235,5 +238,85 @@ public class QuotesController : ControllerBase
return Ok(); return Ok();
} }
[HttpPatch("{id}")]
[Authorize]
[EnableCors]
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
[ProducesResponseType(typeof(ErrorDTO), 400)]
[ProducesResponseType(typeof(ErrorDTO), 404)]
public async Task<IActionResult> 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());
}
} }

View File

@@ -6,7 +6,7 @@ public record class QuoteShortDTO
public string Text { get; set; } = string.Empty; public string Text { get; set; } = string.Empty;
public string Author { get; set; } = string.Empty; public string Author { get; set; } = string.Empty;
public string? ImageUrl { get; set; } public string? ImageUrl { get; set; }
public List<string> Categories { get; set; } = new(); public List<string>? Categories { get; set; } = new();
}; };