categorys

This commit is contained in:
Tizian.Breuch
2025-07-31 15:33:35 +02:00
parent ca1d3fda1d
commit ad7bfeaa40
4 changed files with 97 additions and 45 deletions

View File

@@ -1,45 +1,62 @@
// src/Webshop.Application/Services/Public/ProductService.cs
using Microsoft.EntityFrameworkCore; // << NEU: Für Include() und ThenInclude() >>
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Products;
using Webshop.Application.Services.Public.Interfaces;
using Webshop.Domain.Interfaces;
using Webshop.Application.DTOs.Categorys; // Für CategoryDto
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 >>
namespace Webshop.Application.Services.Public
{
public class ProductService : IProductService
{
private readonly IProductRepository _productRepository;
private readonly ApplicationDbContext _context; // << NEU: Für direkten DB-Zugriff >>
public ProductService(IProductRepository productRepository)
public ProductService(IProductRepository productRepository, ApplicationDbContext context) // << NEU: DbContext injizieren >>
{
_productRepository = productRepository;
_context = context; // << NEU >>
}
// Wir verwenden jetzt den von Ihnen vorgegebenen Namen
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
{
var products = await _productRepository.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.Category) // Lade die zugehörige Kategorie-Entität
.Where(p => p.IsActive) // Nur aktive Produkte
.ToListAsync();
return products
.Where(p => p.IsActive) // Nur aktive Produkte anzeigen
.Select(p => new ProductDto
return products.Select(p => new ProductDto
{
Id = p.Id,
Name = p.Name,
Description = p.ShortDescription, // Oder p.Description, je nach Anforderung
Price = p.Price,
SKU = p.SKU,
ImageUrl = p.ImageUrl,
IsInStock = p.IsInStock,
Slug = p.Slug,
Categories = p.ProductCategories.Select(pc => new CategoryDto
{
Id = p.Id,
Name = p.Name,
Description = p.ShortDescription,
Price = p.Price,
SKU = p.SKU,
ImageUrl = p.ImageUrl,
IsInStock = p.IsInStock,
Slug = p.Slug // Mapping für Slug
}).ToList();
Id = pc.Category.Id,
Name = pc.Category.Name,
Slug = pc.Category.Slug
// ... weitere CategoryDto-Felder bei Bedarf
}).ToList()
}).ToList();
}
// Diese Methode wird hinzugefügt, um das Interface zu erfüllen
public async Task<ProductDto?> GetProductBySlugAsync(string slug)
{
var product = await _productRepository.GetBySlugAsync(slug);
var product = await _context.Products
.Include(p => p.ProductCategories)
.ThenInclude(pc => pc.Category)
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive); // Nur aktives Produkt finden
if (product == null)
{
@@ -50,12 +67,18 @@ namespace Webshop.Application.Services.Public
{
Id = product.Id,
Name = product.Name,
Description = product.Description,
Description = product.Description, // Hier die volle Beschreibung
Price = product.Price,
SKU = product.SKU,
ImageUrl = product.ImageUrl,
IsInStock = product.IsInStock,
Slug = product.Slug // Mapping für Slug
Slug = product.Slug,
Categories = product.ProductCategories.Select(pc => new CategoryDto
{
Id = pc.Category.Id,
Name = pc.Category.Name,
Slug = pc.Category.Slug
}).ToList()
};
}
}