upload bild 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:
@@ -30,14 +30,11 @@ namespace Webshop.Application.Services.Admin
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// ... Ihre GetAll, GetById und Create Methoden bleiben unver<65>ndert ...
|
||||
#region Unchanged Methods
|
||||
public async Task<ServiceResult<IEnumerable<AdminProductDto>>> GetAllAdminProductsAsync()
|
||||
{
|
||||
var products = await _context.Products
|
||||
.Include(p => p.Images)
|
||||
.Include(p => p.Productcategories)
|
||||
.OrderBy(p => p.Name)
|
||||
.ToListAsync();
|
||||
|
||||
var products = await _context.Products.Include(p => p.Images).Include(p => p.Productcategories).OrderBy(p => p.Name).ToListAsync();
|
||||
var dtos = products.Select(MapToAdminDto).ToList();
|
||||
return ServiceResult.Ok<IEnumerable<AdminProductDto>>(dtos);
|
||||
}
|
||||
@@ -45,64 +42,24 @@ namespace Webshop.Application.Services.Admin
|
||||
public async Task<ServiceResult<AdminProductDto>> GetAdminProductByIdAsync(Guid id)
|
||||
{
|
||||
var product = await _productRepository.GetProductByIdForUpdateAsync(id);
|
||||
if (product == null)
|
||||
{
|
||||
return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
|
||||
}
|
||||
if (product == null) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden."); }
|
||||
return ServiceResult.Ok(MapToAdminDto(product));
|
||||
}
|
||||
|
||||
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);
|
||||
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);
|
||||
if (slugExists) { return ServiceResult.Fail<AdminProductDto>(ServiceResultType.Conflict, $"Ein Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); }
|
||||
var images = new List<ProductImage>();
|
||||
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++ });
|
||||
}
|
||||
}
|
||||
}
|
||||
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()
|
||||
};
|
||||
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++ }); } } }
|
||||
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);
|
||||
return ServiceResult.Ok(MapToAdminDto(newProduct));
|
||||
}
|
||||
#endregion
|
||||
|
||||
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);
|
||||
if (skuExists)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit der SKU '{productDto.SKU}' existiert bereits.");
|
||||
}
|
||||
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.");
|
||||
}
|
||||
if (slugExists) { return ServiceResult.Fail(ServiceResultType.Conflict, $"Ein anderes Produkt mit dem Slug '{productDto.Slug}' existiert bereits."); }
|
||||
|
||||
// --- KORRIGIERTER METHODENAUFRUF ---
|
||||
existingProduct.UpdateFromDto(
|
||||
productDto.Name, productDto.Description, productDto.SKU, productDto.Price, productDto.OldPrice,
|
||||
productDto.IsActive, productDto.StockQuantity, productDto.Slug, productDto.Weight,
|
||||
productDto.SupplierId, productDto.PurchasePrice, productDto.IsFeatured, productDto.FeaturedDisplayOrder,
|
||||
productDto.CategorieIds
|
||||
);
|
||||
// --- DIREKTE ZUWEISUNG STATT UpdateFromDto ---
|
||||
existingProduct.Name = productDto.Name;
|
||||
existingProduct.Description = productDto.Description;
|
||||
existingProduct.SKU = productDto.SKU;
|
||||
existingProduct.Price = productDto.Price;
|
||||
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)
|
||||
if (productDto.ImagesToDelete != null && productDto.ImagesToDelete.Any())
|
||||
{
|
||||
var imagesToRemove = existingProduct.Images.Where(img => productDto.ImagesToDelete.Contains(img.Id)).ToList();
|
||||
foreach (var image in imagesToRemove)
|
||||
{
|
||||
existingProduct.Images.Remove(image);
|
||||
foreach (var image in imagesToRemove) { existingProduct.Images.Remove(image); }
|
||||
}
|
||||
}
|
||||
|
||||
if (productDto.MainImageFile != null)
|
||||
{
|
||||
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);
|
||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = true });
|
||||
}
|
||||
|
||||
if (productDto.AdditionalImageFiles != null && productDto.AdditionalImageFiles.Any())
|
||||
{
|
||||
foreach (var file in productDto.AdditionalImageFiles)
|
||||
@@ -160,7 +126,6 @@ namespace Webshop.Application.Services.Admin
|
||||
existingProduct.Images.Add(new ProductImage { Url = url, IsMainImage = false });
|
||||
}
|
||||
}
|
||||
|
||||
int currentOrder = 1;
|
||||
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();
|
||||
}
|
||||
|
||||
#region Unchanged Methods
|
||||
public async Task<ServiceResult> DeleteAdminProductAsync(Guid id)
|
||||
{
|
||||
var product = await _productRepository.GetProductByIdAsync(id);
|
||||
if (product == null)
|
||||
{
|
||||
return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden.");
|
||||
}
|
||||
if (product == null) { return ServiceResult.Fail(ServiceResultType.NotFound, $"Produkt mit ID '{id}' nicht gefunden."); }
|
||||
await _productRepository.DeleteProductAsync(id);
|
||||
return ServiceResult.Ok();
|
||||
}
|
||||
|
||||
private AdminProductDto MapToAdminDto(Product product)
|
||||
{
|
||||
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()
|
||||
};
|
||||
}
|
||||
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() };
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user