diff --git a/Webshop.Application/Services/Admin/AdminProductService.cs b/Webshop.Application/Services/Admin/AdminProductService.cs index 0bf6dd6..88bb174 100644 --- a/Webshop.Application/Services/Admin/AdminProductService.cs +++ b/Webshop.Application/Services/Admin/AdminProductService.cs @@ -111,35 +111,80 @@ namespace Webshop.Application.Services.Admin #region Unchanged Methods public async Task UpdateAdminProductAsync(UpdateAdminProductDto productDto) { - // 1. Produkt laden - var existingProduct = await _context.Products - // 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); + Console.WriteLine($"---- UPDATE START: Produkt-ID {productDto.Id} ----"); + var existingProduct = await _context.Products.Include(p => p.Images).Include(p => p.Productcategories).FirstOrDefaultAsync(p => p.Id == productDto.Id); + if (existingProduct == null) { return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden."); } - if (existingProduct == null) + // SKU/Slug Checks bleiben 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."); } + + // --- BILDER-LOGGING --- + if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any()) { - return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden."); + Console.WriteLine($"---- LÖSCHE {productDto.ImagesToDelete.Count} BILDER ----"); + var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList(); + _context.ProductImages.RemoveRange(imagesToRemove); + } + if (productDto.MainImageFile != null) + { + Console.WriteLine("---- ERSETZE HAUPTBILD ----"); + var existingMainImage = existingProduct.Images.FirstOrDefault(img => img.IsMainImage); + if (existingMainImage != null) _context.ProductImages.Remove(existingMainImage); + + await using var stream = productDto.MainImageFile.OpenReadStream(); + var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType); + Console.WriteLine($"---- NEUE HAUPTBILD-URL: {url} ----"); + existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 }); + } + if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any()) + { + Console.WriteLine($"---- FÜGE {productDto.AdditionalImageFiles.Count} NEUE BILDER HINZU ----"); + int displayOrder = (existingProduct.Images.Any() ? existingProduct.Images.Max(i => i.DisplayOrder) : 0) + 1; + foreach (var file in productDto.AdditionalImageFiles) + { + await using var stream = file.OpenReadStream(); + var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType); + Console.WriteLine($"---- NEUE BILD-URL: {url} ----"); + existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = displayOrder++ }); + } } - // 2. NUR den Namen ändern - Console.WriteLine($"---- DEBUG: Ändere Namen von '{existingProduct.Name}' zu '{productDto.Name}' ----"); + // --- EIGENSCHAFTEN-UPDATE --- + Console.WriteLine("---- AKTUALISIERE PRODUKT-EIGENSCHAFTEN ----"); existingProduct.Name = productDto.Name; + existingProduct.Description = productDto.Description; + existingProduct.SKU = productDto.SKU; + existingProduct.Price = productDto.Price; + existingProduct.IsActive = productDto.IsActive; + existingProduct.StockQuantity = productDto.StockQuantity; + existingProduct.Slug = productDto.Slug; + existingProduct.Weight = productDto.Weight; + existingProduct.OldPrice = productDto.OldPrice; + existingProduct.SupplierId = productDto.SupplierId; + existingProduct.PurchasePrice = productDto.PurchasePrice; existingProduct.LastModifiedDate = DateTimeOffset.UtcNow; + existingProduct.IsFeatured = productDto.IsFeatured; + existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder; - // 3. Speichern - try + // --- KATEGORIEN-UPDATE --- + Console.WriteLine("---- AKTUALISIERE KATEGORIEN ----"); + existingProduct.Productcategories.Clear(); + if (productDto.CategorieIds != null) { - await _context.SaveChangesAsync(); - Console.WriteLine("---- DEBUG: SaveChangesAsync erfolgreich aufgerufen. ----"); - } - catch (Exception ex) - { - Console.WriteLine($"---- DEBUG: FEHLER bei SaveChangesAsync: {ex.Message} ----"); - throw; // Fehler weiterwerfen, damit wir es im Log sehen + foreach (var categorieId in productDto.CategorieIds) + { + existingProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId }); + } } + // --- SPEICHERN --- + Console.WriteLine("---- RUFE SaveChangesAsync AUF ----"); + await _context.SaveChangesAsync(); + Console.WriteLine("---- UPDATE BEENDET ----"); + return ServiceResult.Ok(); }