test
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 26s
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:
@@ -111,34 +111,79 @@ namespace Webshop.Application.Services.Admin
|
|||||||
#region Unchanged Methods
|
#region Unchanged Methods
|
||||||
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
|
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
|
||||||
{
|
{
|
||||||
// 1. Produkt laden
|
Console.WriteLine($"---- UPDATE START: Produkt-ID {productDto.Id} ----");
|
||||||
var existingProduct = await _context.Products
|
var existingProduct = await _context.Products.Include(p => p.Images).Include(p => p.Productcategories).FirstOrDefaultAsync(p => p.Id == productDto.Id);
|
||||||
// Wir brauchen die Includes f<>r diesen Test nicht, das macht es schneller
|
if (existingProduct == null) { return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden."); }
|
||||||
// .Include(p => p.Images)
|
|
||||||
// .Include(p => p.Productcategories)
|
|
||||||
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
|
|
||||||
|
|
||||||
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 <20>ndern
|
// --- EIGENSCHAFTEN-UPDATE ---
|
||||||
Console.WriteLine($"---- DEBUG: <20>ndere Namen von '{existingProduct.Name}' zu '{productDto.Name}' ----");
|
Console.WriteLine("---- AKTUALISIERE PRODUKT-EIGENSCHAFTEN ----");
|
||||||
existingProduct.Name = productDto.Name;
|
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.LastModifiedDate = DateTimeOffset.UtcNow;
|
||||||
|
existingProduct.IsFeatured = productDto.IsFeatured;
|
||||||
|
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
|
||||||
|
|
||||||
// 3. Speichern
|
// --- KATEGORIEN-UPDATE ---
|
||||||
try
|
Console.WriteLine("---- AKTUALISIERE KATEGORIEN ----");
|
||||||
|
existingProduct.Productcategories.Clear();
|
||||||
|
if (productDto.CategorieIds != null)
|
||||||
{
|
{
|
||||||
|
foreach (var categorieId in productDto.CategorieIds)
|
||||||
|
{
|
||||||
|
existingProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- SPEICHERN ---
|
||||||
|
Console.WriteLine("---- RUFE SaveChangesAsync AUF ----");
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
Console.WriteLine("---- DEBUG: SaveChangesAsync erfolgreich aufgerufen. ----");
|
Console.WriteLine("---- UPDATE BEENDET ----");
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"---- DEBUG: FEHLER bei SaveChangesAsync: {ex.Message} ----");
|
|
||||||
throw; // Fehler weiterwerfen, damit wir es im Log sehen
|
|
||||||
}
|
|
||||||
|
|
||||||
return ServiceResult.Ok();
|
return ServiceResult.Ok();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user