diff --git a/Webshop.Application/DTOs/Products/UpdateAdminProductDto.cs b/Webshop.Application/DTOs/Products/UpdateAdminProductDto.cs index e303c2f..6f56787 100644 --- a/Webshop.Application/DTOs/Products/UpdateAdminProductDto.cs +++ b/Webshop.Application/DTOs/Products/UpdateAdminProductDto.cs @@ -36,6 +36,6 @@ namespace Webshop.Application.DTOs.Products public bool IsFeatured { get; set; } public int FeaturedDisplayOrder { get; set; } - public byte[]? RowVersion { get; set; } + public string? RowVersion { get; set; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/AdminProductService.cs b/Webshop.Application/Services/Admin/AdminProductService.cs index 6a096de..1678c0a 100644 --- a/Webshop.Application/Services/Admin/AdminProductService.cs +++ b/Webshop.Application/Services/Admin/AdminProductService.cs @@ -120,12 +120,39 @@ namespace Webshop.Application.Services.Admin return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden."); } - // SCHRITT 2: Setze SOFORT den Concurrency Token am "sauberen" Objekt. - if (productDto.RowVersion != null && productDto.RowVersion.Length > 0) + // --- CONCURRENCY CHECK --- + if (!string.IsNullOrEmpty(productDto.RowVersion)) { - _context.Entry(existingProduct).Property(p => p.RowVersion).OriginalValue = productDto.RowVersion; + try + { + // <<<<<< WICHTIGE KORREKTUR HIER >>>>>> + // FormData wandelt "+" oft in " " um. Das reparieren wir hier: + string fixedRowVersion = productDto.RowVersion.Replace(" ", "+"); + + // 1. Umwandlung von String (Base64) zu Byte-Array + byte[] incomingRowVersion = Convert.FromBase64String(fixedRowVersion); + + // DEBUG-LOGGING + string dbValue = Convert.ToBase64String(existingProduct.RowVersion ?? new byte[0]); + + Console.WriteLine($"DB RowVersion: {dbValue}"); + Console.WriteLine($"Frontend RowVersion: {fixedRowVersion}"); // Logge den reparierten Wert + + if (dbValue != fixedRowVersion) + { + Console.WriteLine("!!! WARNUNG: Versionen stimmen nicht überein !!!"); + } + + // 2. Setze den Originalwert für EF Core + _context.Entry(existingProduct).Property(p => p.RowVersion).OriginalValue = incomingRowVersion; + } + catch (FormatException) + { + return ServiceResult.Fail(ServiceResultType.Failure, "RowVersion Format ist ungültig."); + } } + // SCHRITT 3: Lade jetzt die Relationen explizit nach. await _context.Entry(existingProduct).Collection(p => p.Images).LoadAsync(); await _context.Entry(existingProduct).Collection(p => p.Productcategories).LoadAsync();