image änderungen

This commit is contained in:
Tizian.Breuch
2025-08-06 10:42:32 +02:00
parent 2475e896b9
commit 7ff593cfcf
16 changed files with 427 additions and 172 deletions

View File

@@ -1,33 +1,38 @@
// src/Webshop.Application/Services/Admin/AdminProductService.cs
using Microsoft.EntityFrameworkCore; // << NEU: F<>r Include() und FirstOrDefaultAsync() >>
using Microsoft.EntityFrameworkCore;
using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;
using System.Linq;
using Webshop.Application.DTOs.Products; // F<>r AdminProductDto
using Webshop.Application.Services.Admin.Interfaces; // F<>r IAdminProductService
using Webshop.Infrastructure.Data; // << NEU: F<>r ApplicationDbContext >>
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Admin
{
public class AdminProductService : IAdminProductService
{
private readonly IProductRepository _productRepository;
private readonly ApplicationDbContext _context; // << NEU: F<>r direkten DB-Zugriff auf Join-Tabellen >>
private readonly IFileStorageService _fileStorageService;
private readonly ApplicationDbContext _context;
public AdminProductService(IProductRepository productRepository, ApplicationDbContext context) // << NEU: DbContext injizieren >>
public AdminProductService(
IProductRepository productRepository,
IFileStorageService fileStorageService,
ApplicationDbContext context)
{
_productRepository = productRepository;
_context = context; // << NEU >>
_fileStorageService = fileStorageService;
_context = context;
}
public async Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync()
{
// Wir verwenden den DbContext, um auch die Kategorien effizient mitzuladen
var products = await _context.Products
.Include(p => p.Productcategories)
.Include(p => p.Images)
.ToListAsync();
return products.Select(p => new AdminProductDto
@@ -42,20 +47,27 @@ namespace Webshop.Application.Services.Admin
IsInStock = p.IsInStock,
StockQuantity = p.StockQuantity,
Weight = p.Weight,
ImageUrl = p.ImageUrl,
Slug = p.Slug,
CreatedDate = p.CreatedDate,
LastModifiedDate = p.LastModifiedDate,
SupplierId = p.SupplierId,
PurchasePrice = p.PurchasePrice,
categorieIds = p.Productcategories.Select(pc => pc.categorieId).ToList() // << NEU >>
categorieIds = p.Productcategories.Select(pc => pc.categorieId).ToList(),
Images = p.Images.Select(img => new ProductImageDto
{
Id = img.Id,
Url = img.Url,
IsMainImage = img.IsMainImage,
DisplayOrder = img.DisplayOrder
}).ToList()
}).ToList();
}
public async Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id)
{
var product = await _context.Products
.Include(p => p.Productcategories) // << NEU: Lade die Join-Tabelle mit >>
.Include(p => p.Productcategories)
.Include(p => p.Images)
.FirstOrDefaultAsync(p => p.Id == id);
if (product == null) return null;
@@ -72,83 +84,100 @@ namespace Webshop.Application.Services.Admin
IsInStock = product.IsInStock,
StockQuantity = product.StockQuantity,
Weight = product.Weight,
ImageUrl = product.ImageUrl,
Slug = product.Slug,
CreatedDate = product.CreatedDate,
LastModifiedDate = product.LastModifiedDate,
SupplierId = product.SupplierId,
PurchasePrice = product.PurchasePrice,
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList() // << NEU: Mappe die categorieIds >>
categorieIds = product.Productcategories.Select(pc => pc.categorieId).ToList(),
Images = product.Images.Select(img => new ProductImageDto
{
Id = img.Id,
Url = img.Url,
IsMainImage = img.IsMainImage,
DisplayOrder = img.DisplayOrder
}).ToList()
};
}
public async Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto)
public async Task<AdminProductDto> CreateAdminProductAsync(CreateAdminProductDto productDto)
{
var images = new List<ProductImage>();
if (productDto.MainImageFile != null)
{
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)
{
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
{
Id = Guid.NewGuid(),
Name = productDto.Name,
Description = productDto.Description,
SKU = productDto.SKU,
Price = productDto.Price,
OldPrice = productDto.OldPrice,
IsActive = productDto.IsActive,
IsInStock = productDto.IsInStock,
StockQuantity = productDto.StockQuantity,
Weight = productDto.Weight,
ImageUrl = productDto.ImageUrl,
Slug = productDto.Slug,
CreatedDate = DateTimeOffset.UtcNow,
SupplierId = productDto.SupplierId,
PurchasePrice = productDto.PurchasePrice,
Productcategories = new List<Productcategorie>() // Initialisiere die Collection
Images = images,
Productcategories = productDto.CategorieIds.Select(cId => new Productcategorie { categorieId = cId }).ToList()
};
// << NEU: F<>ge die Kategorien hinzu >>
foreach (var categorieId in productDto.categorieIds)
{
newProduct.Productcategories.Add(new Productcategorie { categorieId = categorieId });
}
await _productRepository.AddProductAsync(newProduct);
await _productRepository.AddProductAsync(newProduct); // << KORREKT: VERWENDET AddProductAsync >>
productDto.Id = newProduct.Id;
return productDto;
return (await GetAdminProductByIdAsync(newProduct.Id))!;
}
public async Task<bool> UpdateAdminProductAsync(AdminProductDto productDto)
public async Task<bool> UpdateAdminProductAsync(UpdateAdminProductDto productDto)
{
var existingProduct = await _context.Products
.Include(p => p.Productcategories) // Lade die aktuellen Zuweisungen
.Include(p => p.Images)
.Include(p => p.Productcategories)
.FirstOrDefaultAsync(p => p.Id == productDto.Id);
if (existingProduct == null) return false;
// Aktualisiere die direkten Eigenschaften des Produkts
if (productDto.ImagesToDelete != null)
{
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)
{
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.OldPrice = productDto.OldPrice;
existingProduct.IsActive = productDto.IsActive;
existingProduct.IsInStock = productDto.IsInStock;
existingProduct.StockQuantity = productDto.StockQuantity;
existingProduct.Weight = productDto.Weight;
existingProduct.ImageUrl = productDto.ImageUrl;
existingProduct.Slug = productDto.Slug;
existingProduct.SupplierId = productDto.SupplierId;
existingProduct.PurchasePrice = productDto.PurchasePrice;
existingProduct.LastModifiedDate = DateTimeOffset.UtcNow;
// ... (restliche Felder aktualisieren) ...
// << NEU: Kategorien synchronisieren (alte l<>schen, neue hinzuf<75>gen) >>
existingProduct.Productcategories.Clear();
foreach (var categorieId in productDto.categorieIds)
{
existingProduct.Productcategories.Add(new Productcategorie { ProductId = existingProduct.Id, categorieId = categorieId });
}
// << ENDE NEUER TEIL >>
await _productRepository.UpdateProductAsync(existingProduct); // << KORREKT: VERWENDET UpdateProductAsync >>
await _productRepository.UpdateProductAsync(existingProduct);
return true;
}
@@ -157,7 +186,7 @@ namespace Webshop.Application.Services.Admin
var product = await _productRepository.GetProductByIdAsync(id);
if (product == null) return false;
await _productRepository.DeleteProductAsync(id); // << KORREKT: VERWENDET DeleteProductAsync >>
await _productRepository.DeleteProductAsync(id);
return true;
}
}

View File

@@ -1,4 +1,4 @@
// src/Webshop.Application/Services/Admin/IAdminProductService.cs
// src/Webshop.Application/Services/Admin/Interfaces/IAdminProductService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Products;
@@ -9,8 +9,8 @@ namespace Webshop.Application.Services.Admin.Interfaces
{
Task<IEnumerable<AdminProductDto>> GetAllAdminProductsAsync();
Task<AdminProductDto?> GetAdminProductByIdAsync(Guid id);
Task<AdminProductDto> CreateAdminProductAsync(AdminProductDto productDto);
Task<bool> UpdateAdminProductAsync(AdminProductDto productDto);
Task<AdminProductDto> CreateAdminProductAsync(CreateAdminProductDto productDto); // << NEUER TYP >>
Task<bool> UpdateAdminProductAsync(UpdateAdminProductDto productDto); // << NEUER TYP >>
Task<bool> DeleteAdminProductAsync(Guid id);
}
}

View File

@@ -1,44 +1,40 @@
// src/Webshop.Application/Services/Public/ProductService.cs
using Microsoft.EntityFrameworkCore; // << NEU: Für Include() und ThenInclude() >>
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Categorie; // Für categorieDto
using Webshop.Application.DTOs.Products; // Für ProductDto
using Webshop.Application.Services.Public.Interfaces; // Für IProductService
using Webshop.Domain.Interfaces; // Für IProductRepository
using Webshop.Infrastructure.Data; // << NEU: Für ApplicationDbContext >>
using Webshop.Application.DTOs.Categorie;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Public.Interfaces;
using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Public
{
public class ProductService : IProductService
{
private readonly IProductRepository _productRepository;
private readonly ApplicationDbContext _context; // << NEU: Für direkten DB-Zugriff >>
private readonly ApplicationDbContext _context;
public ProductService(IProductRepository productRepository, ApplicationDbContext context) // << NEU: DbContext injizieren >>
public ProductService(ApplicationDbContext context)
{
_productRepository = productRepository;
_context = context; // << NEU >>
_context = context;
}
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
{
// Wir verwenden den DbContext, um Produkte und ihre Kategorien zu laden
var products = await _context.Products
.Include(p => p.Productcategories) // Lade die Join-Tabelle
.ThenInclude(pc => pc.categorie) // Lade die zugehörige Kategorie-Entität
.Where(p => p.IsActive) // Nur aktive Produkte
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
.Include(p => p.Images) // Lade Bilder mit
.Where(p => p.IsActive)
.ToListAsync();
return products.Select(p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.ShortDescription, // Oder p.Description, je nach Anforderung
Description = p.ShortDescription,
Price = p.Price,
SKU = p.SKU,
ImageUrl = p.ImageUrl,
IsInStock = p.IsInStock,
Slug = p.Slug,
categories = p.Productcategories.Select(pc => new CategorieDto
@@ -46,7 +42,13 @@ namespace Webshop.Application.Services.Public
Id = pc.categorie.Id,
Name = pc.categorie.Name,
Slug = pc.categorie.Slug
// ... weitere categorieDto-Felder bei Bedarf
}).ToList(),
Images = p.Images.Select(img => new ProductImageDto
{
Id = img.Id,
Url = img.Url,
IsMainImage = img.IsMainImage,
DisplayOrder = img.DisplayOrder
}).ToList()
}).ToList();
}
@@ -54,9 +56,9 @@ namespace Webshop.Application.Services.Public
public async Task<ProductDto?> GetProductBySlugAsync(string slug)
{
var product = await _context.Products
.Include(p => p.Productcategories)
.ThenInclude(pc => pc.categorie)
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive); // Nur aktives Produkt finden
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
.Include(p => p.Images) // Lade Bilder mit
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive);
if (product == null)
{
@@ -67,10 +69,9 @@ namespace Webshop.Application.Services.Public
{
Id = product.Id,
Name = product.Name,
Description = product.Description, // Hier die volle Beschreibung
Description = product.Description,
Price = product.Price,
SKU = product.SKU,
ImageUrl = product.ImageUrl,
IsInStock = product.IsInStock,
Slug = product.Slug,
categories = product.Productcategories.Select(pc => new CategorieDto
@@ -78,6 +79,13 @@ namespace Webshop.Application.Services.Public
Id = pc.categorie.Id,
Name = pc.categorie.Name,
Slug = pc.categorie.Slug
}).ToList(),
Images = product.Images.Select(img => new ProductImageDto
{
Id = img.Id,
Url = img.Url,
IsMainImage = img.IsMainImage,
DisplayOrder = img.DisplayOrder
}).ToList()
};
}