From dae7d1d979b55257426a9c1133642b55193f0d88 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Fri, 7 Nov 2025 14:15:35 +0100 Subject: [PATCH] test --- .../Controllers/Admin/AdminProductsController.cs | 11 +++-------- .../Services/Admin/AdminProductService.cs | 3 ++- Webshop.Domain/Interfaces/IProductRepository.cs | 1 + .../Repositories/ProductRepository.cs | 4 ++++ 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Webshop.Api/Controllers/Admin/AdminProductsController.cs b/Webshop.Api/Controllers/Admin/AdminProductsController.cs index c2375ee..b776b74 100644 --- a/Webshop.Api/Controllers/Admin/AdminProductsController.cs +++ b/Webshop.Api/Controllers/Admin/AdminProductsController.cs @@ -78,16 +78,11 @@ namespace Webshop.Api.Controllers.Admin public async Task UpdateAdminProduct(Guid id, [FromForm] UpdateAdminProductDto productDto) { // ============================================================================== - // DIES IST DIE ENTSCHEIDENDE KORREKTUR - // Wir stellen sicher, dass die ID aus der URL die ID ist, mit der wir arbeiten. + // DEINE PERFEKTE LÖSUNG: URL-ID erzwingen // ============================================================================== - if (id != productDto.Id) - { - // Wenn Frontend und URL sich nicht einig sind, ist die Anfrage fehlerhaft. - return BadRequest(new { Message = "Die ID in der URL stimmt nicht mit der ID im Formular überein." }); - } + productDto.Id = id; - // Ab hier läuft alles wie gehabt. Der Service erhält ein DTO mit der verifizierten ID. + // Jetzt den Service mit dem garantiert korrekten DTO aufrufen. var result = await _adminProductService.UpdateAdminProductAsync(productDto); return result.Type switch diff --git a/Webshop.Application/Services/Admin/AdminProductService.cs b/Webshop.Application/Services/Admin/AdminProductService.cs index c8275bf..52561b0 100644 --- a/Webshop.Application/Services/Admin/AdminProductService.cs +++ b/Webshop.Application/Services/Admin/AdminProductService.cs @@ -122,7 +122,8 @@ namespace Webshop.Application.Services.Admin if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any()) { 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); existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = displayOrder++ }); } } 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; existingProduct.Productcategories.Clear(); if (productDto.CategorieIds != null) { foreach (var categorieId in productDto.CategorieIds) { existingProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId }); } } - await _productRepository.UpdateProductAsync(existingProduct); return ServiceResult.Ok(); + await _productRepository.SaveChangesAsync(); + return ServiceResult.Ok(); } public async Task DeleteAdminProductAsync(Guid id) diff --git a/Webshop.Domain/Interfaces/IProductRepository.cs b/Webshop.Domain/Interfaces/IProductRepository.cs index 9b0a0c7..a8e60ff 100644 --- a/Webshop.Domain/Interfaces/IProductRepository.cs +++ b/Webshop.Domain/Interfaces/IProductRepository.cs @@ -13,6 +13,7 @@ namespace Webshop.Domain.Interfaces Task GetBySlugAsync(string slug); Task AddProductAsync(Product product); Task UpdateProductAsync(Product product); + Task SaveChangesAsync(); Task DeleteProductAsync(Guid id); } } \ No newline at end of file diff --git a/Webshop.Infrastructure/Repositories/ProductRepository.cs b/Webshop.Infrastructure/Repositories/ProductRepository.cs index 4fc5708..59ba231 100644 --- a/Webshop.Infrastructure/Repositories/ProductRepository.cs +++ b/Webshop.Infrastructure/Repositories/ProductRepository.cs @@ -55,5 +55,9 @@ namespace Webshop.Infrastructure.Repositories await _context.SaveChangesAsync(); } } + public async Task SaveChangesAsync() + { + await _context.SaveChangesAsync(); + } } } \ No newline at end of file