bild
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 25s
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 25s
This commit is contained in:
@@ -116,7 +116,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
{
|
{
|
||||||
Console.WriteLine($"---- UPDATE START: Produkt-ID {productDto.Id} ----");
|
Console.WriteLine($"---- UPDATE START: Produkt-ID {productDto.Id} ----");
|
||||||
|
|
||||||
// 1. Produkt aus der DB laden (inklusive aktueller RowVersion)
|
// 1. Produkt laden
|
||||||
var existingProduct = await _context.Products
|
var existingProduct = await _context.Products
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
.Include(p => p.Productcategories)
|
.Include(p => p.Productcategories)
|
||||||
@@ -124,62 +124,59 @@ namespace Webshop.Application.Services.Admin
|
|||||||
|
|
||||||
if (existingProduct == null)
|
if (existingProduct == null)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden.");
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. ROBUSTER CONCURRENCY CHECK (Manuell)
|
// 2. CONCURRENCY CHECK (Manuell & Optional)
|
||||||
// Wir vergleichen die Strings direkt. Das ist sicherer als Byte-Manipulation.
|
// Wenn RowVersion gesendet wird, pr<70>fen wir sie. Wenn nicht (Workaround), <20>berschreiben wir einfach (Last Win).
|
||||||
if (!string.IsNullOrEmpty(productDto.RowVersion))
|
if (!string.IsNullOrEmpty(productDto.RowVersion))
|
||||||
{
|
{
|
||||||
// DB-Wert in String wandeln
|
|
||||||
string dbRowVersion = Convert.ToBase64String(existingProduct.RowVersion ?? new byte[0]);
|
string dbRowVersion = Convert.ToBase64String(existingProduct.RowVersion ?? new byte[0]);
|
||||||
|
|
||||||
// Frontend-Wert bereinigen (Leerzeichen zu Plus, falls URL-Decoded wurde)
|
|
||||||
string incomingRowVersion = productDto.RowVersion.Trim().Replace(" ", "+");
|
string incomingRowVersion = productDto.RowVersion.Trim().Replace(" ", "+");
|
||||||
|
|
||||||
Console.WriteLine($"DB Version: '{dbRowVersion}'");
|
// Einfacher String-Vergleich
|
||||||
Console.WriteLine($"Frontend Version: '{incomingRowVersion}'");
|
|
||||||
|
|
||||||
if (dbRowVersion != incomingRowVersion)
|
if (dbRowVersion != incomingRowVersion)
|
||||||
{
|
{
|
||||||
Console.WriteLine("!!! KONFLIKT: Versionen stimmen nicht <20>berein !!!");
|
Console.WriteLine("!!! KONFLIKT: Versionen unterschiedlich !!!");
|
||||||
return ServiceResult.Fail(ServiceResultType.Conflict, "Das Produkt wurde in der Zwischenzeit von jemand anderem bearbeitet. Bitte laden Sie die Seite neu.");
|
return ServiceResult.Fail(ServiceResultType.Conflict, "Das Produkt wurde zwischenzeitlich bearbeitet.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wenn wir hier sind, ist die Version KORREKT.
|
|
||||||
// Wir m<>ssen OriginalValue NICHT mehr setzen, da existingProduct ja
|
|
||||||
// die aktuelle Version aus der DB hat.
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// SCHRITT A: BILDER UPDATE
|
// SCHRITT A: BILDER UPDATE (ISOLIERT)
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
bool imagesChanged = false;
|
bool imagesChanged = false;
|
||||||
|
|
||||||
// Bilder l<EFBFBD>schen
|
// A1. Bilder zum L<>schen markieren
|
||||||
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
||||||
{
|
{
|
||||||
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
|
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
|
||||||
if (imagesToRemove.Any())
|
if (imagesToRemove.Any())
|
||||||
{
|
{
|
||||||
|
// Wir entfernen sie direkt aus dem Context, nicht nur aus der Liste
|
||||||
_context.ProductImages.RemoveRange(imagesToRemove);
|
_context.ProductImages.RemoveRange(imagesToRemove);
|
||||||
imagesChanged = true;
|
imagesChanged = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Hauptbild ersetzen
|
// A2. Hauptbild
|
||||||
if (productDto.MainImageFile != null)
|
if (productDto.MainImageFile != null)
|
||||||
{
|
{
|
||||||
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
||||||
if (existingMainImage != null) _context.ProductImages.Remove(existingMainImage);
|
if (existingMainImage != null)
|
||||||
|
{
|
||||||
|
_context.ProductImages.Remove(existingMainImage);
|
||||||
|
}
|
||||||
|
|
||||||
await using var stream = productDto.MainImageFile.OpenReadStream();
|
await using var stream = productDto.MainImageFile.OpenReadStream();
|
||||||
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
|
||||||
|
|
||||||
|
// WICHTIG: Wir f<>gen das Bild der Collection hinzu.
|
||||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
||||||
imagesChanged = true;
|
imagesChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Zusatzbilder hinzuf<75>gen
|
// A3. Zusatzbilder
|
||||||
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
|
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
|
||||||
{
|
{
|
||||||
int displayOrder = (existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0) + 1;
|
int displayOrder = (existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0) + 1;
|
||||||
@@ -192,63 +189,49 @@ namespace Webshop.Application.Services.Admin
|
|||||||
imagesChanged = true;
|
imagesChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kategorien aktualisieren
|
// -----------------------------------------------------------------------
|
||||||
if (productDto.CategorieIds != null)
|
// SCHRITT A: SPEICHERN (NUR BILDER!)
|
||||||
{
|
// -----------------------------------------------------------------------
|
||||||
// Bestehende IDs
|
|
||||||
var currentIds = existingProduct.Productcategories.Select(pc => pc.categorieId).ToList();
|
|
||||||
// Neue IDs
|
|
||||||
var newIds = productDto.CategorieIds;
|
|
||||||
|
|
||||||
// Nur <20>ndern, wenn wirklich anders (vermeidet unn<6E>tige DB-Writes)
|
|
||||||
if (!new HashSet<Guid>(currentIds).SetEquals(newIds))
|
|
||||||
{
|
|
||||||
existingProduct.Productcategories.Clear();
|
|
||||||
foreach (var categorieId in newIds)
|
|
||||||
{
|
|
||||||
existingProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// SCHRITT A SPEICHERN (Nur wenn Bilder ge<67>ndert wurden)
|
|
||||||
if (imagesChanged)
|
if (imagesChanged)
|
||||||
{
|
{
|
||||||
// Wir aktualisieren den Zeitstempel manuell, damit der User Feedback hat
|
|
||||||
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
|
||||||
existingProduct.RowVersion = Guid.NewGuid().ToByteArray();
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
// WICHTIG: Wir <20>ndern HIER KEINE Eigenschaften am 'existingProduct' (wie RowVersion oder LastModified).
|
||||||
|
// Dadurch generiert EF Core nur SQL f<>r die Tabelle "ProductImages", aber NICHT f<>r "Products".
|
||||||
|
// Das verhindert den Concurrency-Fehler auf der Product-Tabelle.
|
||||||
|
Console.WriteLine("---- SPEICHERE BILDER ----");
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
// Da wir gespeichert haben, hat das Entity jetzt eine NEUE RowVersion in der DB.
|
|
||||||
// Aber unser EF-Kontext wei<65> das bereits, weil das Objekt getracked ist.
|
// Da sich durch Trigger oder DB-Logik die Version des Produkts ge<EFBFBD>ndert haben K<>NNTE,
|
||||||
|
// laden wir das Produkt jetzt neu, um f<>r Schritt B bereit zu sein.
|
||||||
|
await _context.Entry(existingProduct).ReloadAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler beim Bild-Speichern: {ex.Message}");
|
Console.WriteLine($"Fehler Bild-Save: {ex.Message}");
|
||||||
return ServiceResult.Fail(ServiceResultType.Failure, "Fehler beim Speichern der Bilder.");
|
return ServiceResult.Fail(ServiceResultType.Failure, "Fehler beim Speichern der Bilder.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// SCHRITT B: PRODUKT EIGENSCHAFTEN
|
// SCHRITT B: PRODUKT EIGENSCHAFTEN UPDATE
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
// Check auf Duplikate (SKU/Slug) - nur wenn ge<67>ndert
|
// Kategorien
|
||||||
if (existingProduct.SKU != productDto.SKU)
|
if (productDto.CategorieIds != null)
|
||||||
{
|
{
|
||||||
bool skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU && p.Id != productDto.Id);
|
var currentIds = existingProduct.Productcategories.Select(pc => pc.categorieId).ToList();
|
||||||
if (skuExists) return ServiceResult.Fail(ServiceResultType.Conflict, $"SKU '{productDto.SKU}' existiert bereits.");
|
if (!new HashSet<Guid>(currentIds).SetEquals(productDto.CategorieIds))
|
||||||
|
{
|
||||||
|
existingProduct.Productcategories.Clear();
|
||||||
|
foreach (var cId in productDto.CategorieIds)
|
||||||
|
{
|
||||||
|
existingProduct.Productcategories.Add(new Productcategorie { categorieId = cId });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (existingProduct.Slug != productDto.Slug)
|
// Mapping der einfachen Felder
|
||||||
{
|
|
||||||
bool slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug && p.Id != productDto.Id);
|
|
||||||
if (slugExists) return ServiceResult.Fail(ServiceResultType.Conflict, $"Slug '{productDto.Slug}' existiert bereits.");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Werte <20>bernehmen
|
|
||||||
existingProduct.Name = productDto.Name;
|
existingProduct.Name = productDto.Name;
|
||||||
existingProduct.Description = productDto.Description;
|
existingProduct.Description = productDto.Description;
|
||||||
existingProduct.SKU = productDto.SKU;
|
existingProduct.SKU = productDto.SKU;
|
||||||
@@ -263,20 +246,20 @@ namespace Webshop.Application.Services.Admin
|
|||||||
existingProduct.IsFeatured = productDto.IsFeatured;
|
existingProduct.IsFeatured = productDto.IsFeatured;
|
||||||
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
||||||
|
|
||||||
// Immer aktualisieren
|
// JETZT aktualisieren wir die Metadaten des Produkts
|
||||||
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||||
existingProduct.RowVersion = Guid.NewGuid().ToByteArray();
|
existingProduct.RowVersion = Guid.NewGuid().ToByteArray();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine("---- RUFE SaveChangesAsync (Properties) AUF ----");
|
Console.WriteLine("---- SPEICHERE PRODUKT-DATEN ----");
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
// Sollte dank des manuellen Checks oben kaum noch passieren,
|
// Da wir oben ReloadAsync gemacht haben, sollte das hier nur passieren,
|
||||||
// es sei denn, millisekundengenaue Race-Condition.
|
// wenn in der Millisekunde zwischen Bild-Upload und Text-Update jemand anders gespeichert hat.
|
||||||
return ServiceResult.Fail(ServiceResultType.Conflict, "Konflikt beim Speichern der Daten.");
|
return ServiceResult.Fail(ServiceResultType.Conflict, "Konflikt beim Speichern der Produktdaten.");
|
||||||
}
|
}
|
||||||
|
|
||||||
return ServiceResult.Ok();
|
return ServiceResult.Ok();
|
||||||
|
|||||||
Reference in New Issue
Block a user