This commit is contained in:
Tizian.Breuch
2025-09-25 17:03:28 +02:00
parent eb1f58d81f
commit ee21d0573d

View File

@@ -17,7 +17,7 @@ namespace Webshop.Application.Services.Admin
{ {
private readonly IProductRepository _productRepository; private readonly IProductRepository _productRepository;
private readonly IFileStorageService _fileStorageService; private readonly IFileStorageService _fileStorageService;
private readonly ApplicationDbContext _context; // For checks that might not be in the repository private readonly ApplicationDbContext _context;
public AdminProductService( public AdminProductService(
IProductRepository productRepository, IProductRepository productRepository,
@@ -29,32 +29,22 @@ namespace Webshop.Application.Services.Admin
_context = context; _context = context;
} }
// ... (GetAllAdminProductsAsync und GetAdminProductByIdAsync bleiben unver<65>ndert) ...
#region Unchanged Methods
public async Task<ServiceResult<IEnumerable<AdminProductDto>>> GetAllAdminProductsAsync() public async Task<ServiceResult<IEnumerable<AdminProductDto>>> GetAllAdminProductsAsync()
{ {
var products = await _context.Products var products = await _context.Products.Include(p => p.Productcategories).Include(p => p.Images).OrderBy(p => p.Name).ToListAsync();
.Include(p => p.Productcategories)
.Include(p => p.Images)
.OrderBy(p => p.Name)
.ToListAsync();
var dtos = products.Select(MapToAdminDto).ToList(); var dtos = products.Select(MapToAdminDto).ToList();
return ServiceResult.Ok<IEnumerable<AdminProductDto>>(dtos); return ServiceResult.Ok<IEnumerable<AdminProductDto>>(dtos);
} }
public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id) public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id)
{ {
var product = await _context.Products var product = await _context.Products.Include(p => p.Productcategories).Include(p => p.Images).FirstOrDefaultAsync(p => p.Id == id);
.Include(p => p.Productcategories) if (product == null) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden."); }
.Include(p => p.Images)
.FirstOrDefaultAsync(p => p.Id == id);
if (product == null)
{
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToAdminDto(product)); return ServiceResult.Ok(MapToAdminDto(product));
} }
#endregion
public async Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto) public async Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto)
{ {
@@ -70,23 +60,29 @@ namespace Webshop.Application.Services.Admin
} }
var images = new List<ProductImage>(); var images = new List<ProductImage>();
// Bild-Upload Logik...
if (productDto.MainImageFile != null) // << KORREKTUR: Null-Pr<50>fung f<>r das Hauptbild >>
if (productDto.MainImageFile != null && productDto.MainImageFile.Length > 0)
{ {
await using var stream = productDto.MainImageFile.OpenReadStream(); await using var stream = productDto.MainImageFile.OpenReadStream();
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType); var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 }); images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
} }
// << KORREKTUR: Null-Pr<50>fung f<>r zus<75>tzliche Bilder >>
if (productDto.AdditionalImageFiles != null) if (productDto.AdditionalImageFiles != null)
{ {
int order = 2; int order = 2;
foreach (var file in productDto.AdditionalImageFiles) foreach (var file in productDto.AdditionalImageFiles)
{
if (file != null && file.Length > 0)
{ {
await using var stream = file.OpenReadStream(); await using var stream = file.OpenReadStream();
var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType); var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = order++ }); images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = order++ });
} }
} }
}
var newProduct = new Product var newProduct = new Product
{ {
@@ -108,131 +104,38 @@ namespace Webshop.Application.Services.Admin
}; };
await _productRepository.AddProductAsync(newProduct); await _productRepository.AddProductAsync(newProduct);
return ServiceResult.Ok(MapToAdminDto(newProduct)); return ServiceResult.Ok(MapToAdminDto(newProduct));
} }
// ... (UpdateAdminProductAsync und DeleteAdminProductAsync und MapToAdminDto bleiben unver<65>ndert) ...
#region Unchanged Methods
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto) public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
{ {
var existingProduct = await _context.Products var existingProduct = await _context.Products.Include(p => p.Images).Include(p => p.Productcategories).FirstOrDefaultAsync(p => p.Id == productDto.Id);
.Include(p => p.Images) if (existingProduct == null) { return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{productDto.Id}' nicht gefunden."); }
.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.");
}
var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU && p.Id != productDto.Id); var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU && p.Id != productDto.Id);
if (skuExists) if (skuExists) { return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit der SKU '{productDto.SKU}' existiert bereits."); }
{
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); var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug && p.Id != productDto.Id);
if (slugExists) if (slugExists) { return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); }
{ if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any()) { var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList(); _context.ProductImages.RemoveRange(imagesToRemove); }
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); if (productDto.MainImageFile != null) { 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); existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 }); }
} 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;
// Bild-Management Logik... existingProduct.Productcategories.Clear(); if (productDto.CategorieIds != null) { foreach (var categorieId in productDto.CategorieIds) { existingProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId }); } }
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any()) await _productRepository.UpdateProductAsync(existingProduct); return ServiceResult.Ok();
{
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
_context.ProductImages.RemoveRange(imagesToRemove);
}
if (productDto.MainImageFile != null)
{
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);
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
}
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++ });
}
}
// Basisdaten aktualisieren...
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;
// Kategorien synchronisieren
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();
} }
public async Task<ServiceResult> DeleteAdminProductAsync(Guid id) public async Task<ServiceResult> DeleteAdminProductAsync(Guid id)
{ {
var product = await _productRepository.GetProductByIdAsync(id); var product = await _productRepository.GetProductByIdAsync(id);
if (product == null) if (product == null) { return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden."); }
{ await _productRepository.DeleteProductAsync(id); return ServiceResult.Ok();
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
}
await _productRepository.DeleteProductAsync(id);
return ServiceResult.Ok();
} }
private AdminProductDto MapToAdminDto(Product product) private AdminProductDto MapToAdminDto(Product product)
{ {
return new AdminProductDto return new AdminProductDto { Id = product.Id, Name = product.Name, Description = product.Description, SKU = product.SKU, Price = product.Price, OldPrice = product.OldPrice, IsActive = product.IsActive, IsInStock = product.IsInStock, StockQuantity = product.StockQuantity, Weight = product.Weight, Slug = product.Slug, CreatedDate = product.CreatedDate, LastModifiedDate = product.LastModifiedDate, SupplierId = product.SupplierId, PurchasePrice = product.PurchasePrice, IsFeatured = product.IsFeatured, FeaturedDisplayOrder = product.FeaturedDisplayOrder, categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList(), Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto { Id = img.Id, Url = img.Url, IsMainImage = img.IsMainImage, DisplayOrder = img.DisplayOrder }).ToList() };
{ }
Id = product.Id, #endregion
Name = product.Name,
Description = product.Description,
SKU = product.SKU,
Price = product.Price,
OldPrice = product.OldPrice,
IsActive = product.IsActive,
IsInStock = product.IsInStock,
StockQuantity = product.StockQuantity,
Weight = product.Weight,
Slug = product.Slug,
CreatedDate = product.CreatedDate,
LastModifiedDate = product.LastModifiedDate,
SupplierId = product.SupplierId,
PurchasePrice = product.PurchasePrice,
IsFeatured = product.IsFeatured,
FeaturedDisplayOrder = product.FeaturedDisplayOrder,
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList(),
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
{
Id = img.Id,
Url = img.Url,
IsMainImage = img.IsMainImage,
DisplayOrder = img.DisplayOrder
}).ToList()
};
}
} }
} }