bilder
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 27s
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 27s
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 laden
|
// 1. Produkt laden (nur um sicherzugehen, dass es existiert und f<>r alte Bilder)
|
||||||
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)
|
||||||
@@ -127,14 +127,12 @@ namespace Webshop.Application.Services.Admin
|
|||||||
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt nicht gefunden.");
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. CONCURRENCY CHECK (Manuell & Optional)
|
// 2. Optionaler Concurrency Check
|
||||||
// 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))
|
||||||
{
|
{
|
||||||
string dbRowVersion = Convert.ToBase64String(existingProduct.RowVersion ?? new byte[0]);
|
string dbRowVersion = Convert.ToBase64String(existingProduct.RowVersion ?? new byte[0]);
|
||||||
string incomingRowVersion = productDto.RowVersion.Trim().Replace(" ", "+");
|
string incomingRowVersion = productDto.RowVersion.Trim().Replace(" ", "+");
|
||||||
|
|
||||||
// Einfacher String-Vergleich
|
|
||||||
if (dbRowVersion != incomingRowVersion)
|
if (dbRowVersion != incomingRowVersion)
|
||||||
{
|
{
|
||||||
Console.WriteLine("!!! KONFLIKT: Versionen unterschiedlich !!!");
|
Console.WriteLine("!!! KONFLIKT: Versionen unterschiedlich !!!");
|
||||||
@@ -143,25 +141,39 @@ namespace Webshop.Application.Services.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// SCHRITT A: BILDER UPDATE (ISOLIERT)
|
// SCHRITT A: BILDER UPDATE (DIREKT AM CONTEXT)
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
bool imagesChanged = false;
|
bool imagesChanged = false;
|
||||||
|
|
||||||
// A1. Bilder zum L<>schen markieren
|
// A1. Bilder l<EFBFBD>schen
|
||||||
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
||||||
{
|
{
|
||||||
|
// Wir suchen die zu l<>schenden Bilder direkt im Context
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Hilfsfunktion um saubere neue Bilder zu erstellen
|
||||||
|
ProductImage CreateNewImage(string url, bool isMain, int order)
|
||||||
|
{
|
||||||
|
return new ProductImage
|
||||||
|
{
|
||||||
|
Id = Guid.NewGuid(), // WICHTIG: Neue ID client-seitig generieren
|
||||||
|
ProductId = existingProduct.Id, // WICHTIG: Explizite Zuordnung zum Produkt
|
||||||
|
Url = url,
|
||||||
|
IsMainImage = isMain,
|
||||||
|
DisplayOrder = order
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// A2. Hauptbild
|
// A2. Hauptbild
|
||||||
if (productDto.MainImageFile != null)
|
if (productDto.MainImageFile != null)
|
||||||
{
|
{
|
||||||
|
// Altes Hauptbild finden und l<>schen
|
||||||
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage);
|
||||||
if (existingMainImage != null)
|
if (existingMainImage != null)
|
||||||
{
|
{
|
||||||
@@ -171,53 +183,65 @@ namespace Webshop.Application.Services.Admin
|
|||||||
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.
|
// NEU: Direktes Hinzuf<75>gen zum Context (Umgeht Collection-Tracking Probleme)
|
||||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
|
var newMainImage = CreateNewImage(url, true, 1);
|
||||||
|
_context.ProductImages.Add(newMainImage);
|
||||||
|
|
||||||
imagesChanged = true;
|
imagesChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// A3. Zusatzbilder
|
// 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;
|
// H<>chste DisplayOrder ermitteln (sicher gegen NullReference)
|
||||||
|
int currentMaxOrder = existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0;
|
||||||
|
int displayOrder = currentMaxOrder + 1;
|
||||||
|
|
||||||
foreach (var file in productDto.AdditionalImageFiles)
|
foreach (var file in productDto.AdditionalImageFiles)
|
||||||
{
|
{
|
||||||
await using var stream = file.OpenReadStream();
|
await using var stream = file.OpenReadStream();
|
||||||
var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
|
var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
|
||||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = displayOrder++ });
|
|
||||||
|
// NEU: Direktes Hinzuf<75>gen zum Context
|
||||||
|
var newAdditionalImage = CreateNewImage(url, false, displayOrder++);
|
||||||
|
_context.ProductImages.Add(newAdditionalImage);
|
||||||
}
|
}
|
||||||
imagesChanged = true;
|
imagesChanged = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
// SCHRITT A: SPEICHERN (NUR BILDER!)
|
// SCHRITT A SPEICHERN
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
if (imagesChanged)
|
if (imagesChanged)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// WICHTIG: Wir <20>ndern HIER KEINE Eigenschaften am 'existingProduct' (wie RowVersion oder LastModified).
|
Console.WriteLine("---- SPEICHERE BILDER (Context Add) ----");
|
||||||
// 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 sich durch Trigger oder DB-Logik die Version des Produkts ge<67>ndert haben K<>NNTE,
|
// WICHTIG: State neu laden f<>r Schritt B
|
||||||
// laden wir das Produkt jetzt neu, um f<>r Schritt B bereit zu sein.
|
// Wir m<>ssen dem Context sagen, dass existingProduct neu geladen werden soll,
|
||||||
|
// damit die Navigations-Properties (Images) wieder aktuell sind.
|
||||||
await _context.Entry(existingProduct).ReloadAsync();
|
await _context.Entry(existingProduct).ReloadAsync();
|
||||||
|
|
||||||
|
// Da wir die Collection umgangen haben, m<>ssen wir sie explizit neu laden
|
||||||
|
await _context.Entry(existingProduct).Collection(p => p.Images).LoadAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Console.WriteLine($"Fehler Bild-Save: {ex.Message}");
|
Console.WriteLine($"Fehler Bild-Save: {ex.Message}");
|
||||||
|
// Inner Exception loggen, falls vorhanden
|
||||||
|
if (ex.InnerException != null) Console.WriteLine($"Inner: {ex.InnerException.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 UPDATE
|
// SCHRITT B: PRODUKT EIGENSCHAFTEN
|
||||||
// -----------------------------------------------------------------------
|
// -----------------------------------------------------------------------
|
||||||
|
|
||||||
// Kategorien
|
// Kategorien update
|
||||||
if (productDto.CategorieIds != null)
|
if (productDto.CategorieIds != null)
|
||||||
{
|
{
|
||||||
var currentIds = existingProduct.Productcategories.Select(pc => pc.categorieId).ToList();
|
var currentIds = existingProduct.Productcategories.Select(pc => pc.categorieId).ToList();
|
||||||
@@ -231,7 +255,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mapping der einfachen Felder
|
// Mapping
|
||||||
existingProduct.Name = productDto.Name;
|
existingProduct.Name = productDto.Name;
|
||||||
existingProduct.Description = productDto.Description;
|
existingProduct.Description = productDto.Description;
|
||||||
existingProduct.SKU = productDto.SKU;
|
existingProduct.SKU = productDto.SKU;
|
||||||
@@ -246,7 +270,7 @@ namespace Webshop.Application.Services.Admin
|
|||||||
existingProduct.IsFeatured = productDto.IsFeatured;
|
existingProduct.IsFeatured = productDto.IsFeatured;
|
||||||
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
||||||
|
|
||||||
// JETZT aktualisieren wir die Metadaten des Produkts
|
// Metadaten
|
||||||
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||||
existingProduct.RowVersion = Guid.NewGuid().ToByteArray();
|
existingProduct.RowVersion = Guid.NewGuid().ToByteArray();
|
||||||
|
|
||||||
@@ -257,8 +281,6 @@ namespace Webshop.Application.Services.Admin
|
|||||||
}
|
}
|
||||||
catch (DbUpdateConcurrencyException)
|
catch (DbUpdateConcurrencyException)
|
||||||
{
|
{
|
||||||
// Da wir oben ReloadAsync gemacht haben, sollte das hier nur passieren,
|
|
||||||
// wenn in der Millisekunde zwischen Bild-Upload und Text-Update jemand anders gespeichert hat.
|
|
||||||
return ServiceResult.Fail(ServiceResultType.Conflict, "Konflikt beim Speichern der Produktdaten.");
|
return ServiceResult.Fail(ServiceResultType.Conflict, "Konflikt beim Speichern der Produktdaten.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user