From 066a3f438959cf21518d0a0b8463b84d66faa066 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 7 Nov 2025 14:37:03 +0100 Subject: [PATCH] rest --- .../Services/Admin/AdminProductService.cs | 59 ++++--------------- 1 file changed, 12 insertions(+), 47 deletions(-) diff --git a/Webshop.Application/Services/Admin/AdminProductService.cs b/Webshop.Application/Services/Admin/AdminProductService.cs index 2724d22..0bf6dd6 100644 --- a/Webshop.Application/Services/Admin/AdminProductService.cs +++ b/Webshop.Application/Services/Admin/AdminProductService.cs @@ -111,10 +111,11 @@ namespace Webshop.Application.Services.Admin #region Unchanged Methods public async Task 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ü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 ändern + Console.WriteLine($"---- DEBUG: Ä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) { /* ... */ } - // ============================================================================== - // Ä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ö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();