rest
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 26s

This commit is contained in:
Tizian.Breuch
2025-11-07 14:37:03 +01:00
parent 6e5d08a8f4
commit 066a3f4389

View File

@@ -111,10 +111,11 @@ namespace Webshop.Application.Services.Admin
#region Unchanged Methods
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
{
// 1. Produkt laden (bleibt gleich)
// 1. Produkt laden
var existingProduct = await _context.Products
.Include(p => p.Images)
.Include(p => p.Productcategories)
// Wir brauchen die Includes f<>r diesen Test nicht, das macht es schneller
// .Include(p => p.Images)
// .Include(p => p.Productcategories)
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
if (existingProduct == null)
@@ -122,57 +123,21 @@ namespace Webshop.Application.Services.Admin
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden.");
}
// 2. Pr<EFBFBD>fungen (bleibt gleich)
var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU && p.Id != productDto.Id);
if (skuExists) { return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit der SKU '{productDto.SKU}' existiert bereits."); }
var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug && p.Id != productDto.Id);
if (slugExists) { return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); }
// 3. Modifikationen (bleibt gleich)
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any()) { /* ... */ }
if (productDto.MainImageFile != null) { /* ... */ }
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any()) { /* ... */ }
// 2. NUR den Namen <20>ndern
Console.WriteLine($"---- DEBUG: <20>ndere Namen von '{existingProduct.Name}' zu '{productDto.Name}' ----");
existingProduct.Name = productDto.Name;
existingProduct.Description = productDto.Description;
// ... all deine anderen Zuweisungen ...
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
existingProduct.Productcategories.Clear();
if (productDto.CategorieIds != null) { /* ... */ }
// ==============================================================================
// <20>NDERUNG HIER: Wir rufen SaveChanges direkt auf dem Context auf.
// Das eliminiert das Repository als m<>gliche Fehlerquelle.
// ==============================================================================
// 3. Speichern
try
{
// await _productRepository.SaveChangesAsync(); // Alte Zeile
await _context.SaveChangesAsync(); // NEUE ZEILE
await _context.SaveChangesAsync();
Console.WriteLine("---- DEBUG: SaveChangesAsync erfolgreich aufgerufen. ----");
}
catch (DbUpdateConcurrencyException ex)
catch (Exception ex)
{
// Wir fangen den Fehler hier ab, um mehr Details zu loggen (optional aber hilfreich)
Console.WriteLine("---- DbUpdateConcurrencyException Details ----");
foreach (var entry in ex.Entries)
{
if (entry.Entity is Product product)
{
var databaseValues = await entry.GetDatabaseValuesAsync();
if (databaseValues == null)
{
Console.WriteLine($"Das Produkt mit ID {product.Id} wurde in der DB gel<65>scht.");
}
else
{
Console.WriteLine($"Konflikt beim Produkt mit ID {product.Id}.");
// Hier k<>nntest du noch Original- vs. Datenbankwerte loggen
}
}
}
Console.WriteLine("--------------------------------------------");
// Wir werfen den Fehler weiter, damit die App wie bisher reagiert
throw;
Console.WriteLine($"---- DEBUG: FEHLER bei SaveChangesAsync: {ex.Message} ----");
throw; // Fehler weiterwerfen, damit wir es im Log sehen
}
return ServiceResult.Ok();