upload bild test
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:
Tizian.Breuch
2025-11-07 10:23:58 +01:00
parent 9ada5633bc
commit 4d4d7b730a

View File

@@ -30,14 +30,11 @@ namespace Webshop.Application.Services.Admin
_context = context; _context = context;
} }
// ... Ihre GetAll, GetById und Create Methoden 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.Images).Include(p => p.Productcategories).OrderBy(p => p.Name).ToListAsync();
.Include(p => p.Images)
.Include(p => p.Productcategories)
.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);
} }
@@ -45,64 +42,24 @@ namespace Webshop.Application.Services.Admin
public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id) public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id)
{ {
var product = await _productRepository.GetProductByIdForUpdateAsync(id); var product = await _productRepository.GetProductByIdForUpdateAsync(id);
if (product == null) if (product == null) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden."); }
{
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
}
return ServiceResult.Ok(MapToAdminDto(product)); return ServiceResult.Ok(MapToAdminDto(product));
} }
public async Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto) public async Task<ServiceResult<AdminProductDto>> CreateAdminProductAsync(CreateAdminProductDto productDto)
{ {
// Ihre Create-Logik hier... (unver<65>ndert)
var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU); var skuExists = await _context.Products.AnyAsync(p => p.SKU == productDto.SKU);
if (skuExists) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit der SKU '{productDto.SKU}' existiert bereits."); } if (skuExists) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit der SKU '{productDto.SKU}' existiert bereits."); }
var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug); var slugExists = await _context.Products.AnyAsync(p => p.Slug == productDto.Slug);
if (slugExists) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); } if (slugExists) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); }
var images = new List<ProductImage>(); var images = new List<ProductImage>();
if (productDto.MainImageFile != null && productDto.MainImageFile.Length > 0) if (productDto.MainImageFile != null && productDto.MainImageFile.Length > 0) { await using var stream = productDto.MainImageFile.OpenReadStream(); var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType); images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 }); }
{ if (productDto.AdditionalImageFiles != null) { int order = 2; foreach (var file in productDto.AdditionalImageFiles) { if (file != null && file.Length > 0) { await using var stream = file.OpenReadStream(); var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType); images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = order++ }); } } }
await using var stream = productDto.MainImageFile.OpenReadStream(); var newProduct = new Product { Name = productDto.Name, Description = productDto.Description, SKU = productDto.SKU, Price = productDto.Price, IsActive = productDto.IsActive, StockQuantity = productDto.StockQuantity, IsInStock = productDto.StockQuantity > 0, Slug = productDto.Slug, Weight = productDto.Weight, OldPrice = productDto.OldPrice, SupplierId = productDto.SupplierId, PurchasePrice = productDto.PurchasePrice, IsFeatured = productDto.IsFeatured, FeaturedDisplayOrder = productDto.FeaturedDisplayOrder, Images = images, Productcategories = productDto.CategorieIds.Select(cId => new Productcategorie { categorieId = cId }).ToList() };
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
images.Add(new ProductImage { Url = url, IsMainImage = true, DisplayOrder = 1 });
}
if (productDto.AdditionalImageFiles != null)
{
int order = 2;
foreach (var file in productDto.AdditionalImageFiles)
{
if (file != null && file.Length > 0)
{
await using var stream = file.OpenReadStream();
var url = await _fileStorageService.SaveFileAsync(stream, file.FileName, file.ContentType);
images.Add(new ProductImage { Url = url, IsMainImage = false, DisplayOrder = order++ });
}
}
}
var newProduct = new Product
{
Name = productDto.Name,
Description = productDto.Description,
SKU = productDto.SKU,
Price = productDto.Price,
IsActive = productDto.IsActive,
StockQuantity = productDto.StockQuantity,
IsInStock = productDto.StockQuantity > 0,
Slug = productDto.Slug,
Weight = productDto.Weight,
OldPrice = productDto.OldPrice,
SupplierId = productDto.SupplierId,
PurchasePrice = productDto.PurchasePrice,
IsFeatured = productDto.IsFeatured,
FeaturedDisplayOrder = productDto.FeaturedDisplayOrder,
Images = images,
Productcategories = productDto.CategorieIds.Select(cId => new Productcategorie { categorieId = cId }).ToList()
};
await _productRepository.AddProductAsync(newProduct); await _productRepository.AddProductAsync(newProduct);
return ServiceResult.Ok(MapToAdminDto(newProduct)); return ServiceResult.Ok(MapToAdminDto(newProduct));
} }
#endregion
public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto) public async Task<ServiceResult> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
{ {
@@ -113,34 +70,44 @@ namespace Webshop.Application.Services.Admin
} }
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."); }
{
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits.");
}
// --- KORRIGIERTER METHODENAUFRUF --- // --- DIREKTE ZUWEISUNG STATT UpdateFromDto ---
existingProduct.UpdateFromDto( existingProduct.Name = productDto.Name;
productDto.Name, productDto.Description, productDto.SKU, productDto.Price, productDto.OldPrice, existingProduct.Description = productDto.Description;
productDto.IsActive, productDto.StockQuantity, productDto.Slug, productDto.Weight, existingProduct.SKU = productDto.SKU;
productDto.SupplierId, productDto.PurchasePrice, productDto.IsFeatured, productDto.FeaturedDisplayOrder, existingProduct.Price = productDto.Price;
productDto.CategorieIds existingProduct.OldPrice = productDto.OldPrice;
); existingProduct.IsActive = productDto.IsActive;
existingProduct.StockQuantity = productDto.StockQuantity;
existingProduct.IsInStock = productDto.StockQuantity > 0;
existingProduct.Slug = productDto.Slug;
existingProduct.Weight = productDto.Weight;
existingProduct.SupplierId = productDto.SupplierId;
existingProduct.PurchasePrice = productDto.PurchasePrice;
existingProduct.IsFeatured = productDto.IsFeatured;
existingProduct.FeaturedDisplayOrder = productDto.FeaturedDisplayOrder;
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
// Kategorien direkt bearbeiten
existingProduct.Productcategories.Clear();
if (productDto.CategorieIds != null)
{
foreach (var catId in productDto.CategorieIds)
{
existingProduct.Productcategories.Add(new Productcategorie { categorieId = catId });
}
}
// --- ENDE DIREKTE ZUWEISUNG ---
// Logik f<>r Bilder (unver<65>ndert) // Logik f<>r Bilder (unver<65>ndert)
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any()) if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
{ {
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList(); var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
foreach (var image in imagesToRemove) foreach (var image in imagesToRemove) { existingProduct.Images.Remove(image); }
{
existingProduct.Images.Remove(image);
} }
}
if (productDto.MainImageFile != null) if (productDto.MainImageFile != null)
{ {
var oldMainImage = existingProduct.Images.FirstOrDefault(i => i.IsMainImage); var oldMainImage = existingProduct.Images.FirstOrDefault(i => i.IsMainImage);
@@ -150,7 +117,6 @@ namespace Webshop.Application.Services.Admin
var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType); var url = await _fileStorageService.SaveFileAsync(stream, productDto.MainImageFile.FileName, productDto.MainImageFile.ContentType);
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true }); existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true });
} }
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any()) if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
{ {
foreach (var file in productDto.AdditionalImageFiles) foreach (var file in productDto.AdditionalImageFiles)
@@ -160,7 +126,6 @@ namespace Webshop.Application.Services.Admin
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false }); existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false });
} }
} }
int currentOrder = 1; int currentOrder = 1;
foreach (var image in existingProduct.Images.OrderBy(img => !img.IsMainImage).ThenBy(img => img.Id)) foreach (var image in existingProduct.Images.OrderBy(img => !img.IsMainImage).ThenBy(img => img.Id))
{ {
@@ -172,43 +137,19 @@ namespace Webshop.Application.Services.Admin
return ServiceResult.Ok(); return ServiceResult.Ok();
} }
#region Unchanged Methods
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."); }
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
}
await _productRepository.DeleteProductAsync(id); await _productRepository.DeleteProductAsync(id);
return ServiceResult.Ok(); 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,
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()
};
} }
#endregion
} }
} }