image änderungen
This commit is contained in:
@@ -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()
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user