Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9ab23ef29a | |||
| 6353591638 |
@@ -1,4 +1,5 @@
|
|||||||
// src/Webshop.Application/Services/Public/ProductService.cs
|
// /src/Webshop.Application/Services/Public/ProductService.cs
|
||||||
|
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -12,6 +13,10 @@ using Webshop.Infrastructure.Data;
|
|||||||
|
|
||||||
namespace Webshop.Application.Services.Public
|
namespace Webshop.Application.Services.Public
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Service, der für die Abfrage von Produktinformationen für den öffentlichen Shop-Bereich zuständig ist.
|
||||||
|
/// Alle hier zurückgegebenen Daten sind für jeden Besucher sichtbar.
|
||||||
|
/// </summary>
|
||||||
public class ProductService : IProductService
|
public class ProductService : IProductService
|
||||||
{
|
{
|
||||||
private readonly ApplicationDbContext _context;
|
private readonly ApplicationDbContext _context;
|
||||||
@@ -21,53 +26,96 @@ namespace Webshop.Application.Services.Public
|
|||||||
_context = context;
|
_context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ruft eine Liste aller aktiven Produkte für die allgemeine Produktübersicht ab.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Eine Liste von ProductDto-Objekten.</returns>
|
||||||
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetAllProductsAsync()
|
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetAllProductsAsync()
|
||||||
{
|
{
|
||||||
|
// Startet eine Abfrage auf die 'Products'-Tabelle in der Datenbank.
|
||||||
var products = await _context.Products
|
var products = await _context.Products
|
||||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
// Lädt die zugehörigen 'Productcategories'-Verknüpfungen mit.
|
||||||
|
.Include(p => p.Productcategories)
|
||||||
|
// Lädt von dort aus die eigentliche 'categorie'-Entität.
|
||||||
|
.ThenInclude(pc => pc.categorie)
|
||||||
|
// Lädt auch die zugehörigen Produktbilder mit.
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
|
// Filtert die Ergebnisse: Nur Produkte, die als 'IsActive' markiert sind.
|
||||||
.Where(p => p.IsActive)
|
.Where(p => p.IsActive)
|
||||||
|
// Führt die Abfrage auf der Datenbank aus und lädt die Ergebnisse in eine Liste.
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Wandelt jede Product-Entität in ein ProductDto um.
|
||||||
|
// 'useShortDescription: true' sorgt dafür, dass die kürzere Beschreibung für die Listenansicht verwendet wird.
|
||||||
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
||||||
|
|
||||||
|
// Gibt das erfolgreiche Ergebnis mit der DTO-Liste zurück.
|
||||||
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ruft ein einzelnes, aktives Produkt anhand seines eindeutigen Slugs ab (für die Produktdetailseite).
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="slug">Der URL-freundliche Slug des Produkts.</param>
|
||||||
|
/// <returns>Ein einzelnes ProductDto oder ein NotFound-Ergebnis.</returns>
|
||||||
public async Task<ServiceResult<ProductDto>> GetProductBySlugAsync(string slug)
|
public async Task<ServiceResult<ProductDto>> GetProductBySlugAsync(string slug)
|
||||||
{
|
{
|
||||||
|
// Startet eine Abfrage auf die 'Products'-Tabelle.
|
||||||
var product = await _context.Products
|
var product = await _context.Products
|
||||||
|
// Lädt alle notwendigen verknüpften Daten für die Detailansicht.
|
||||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
|
// Findet das erste Produkt, das dem Slug entspricht UND aktiv ist.
|
||||||
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive);
|
.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive);
|
||||||
|
|
||||||
|
// Wenn kein Produkt gefunden wurde (oder es inaktiv ist), wird ein Fehler zurückgegeben.
|
||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
return ServiceResult.Fail<ProductDto>(ServiceResultType.NotFound, $"Produkt mit dem Slug '{slug}' wurde nicht gefunden.");
|
return ServiceResult.Fail<ProductDto>(ServiceResultType.NotFound, $"Produkt mit dem Slug '{slug}' wurde nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wandelt die gefundene Entität in ein DTO um.
|
||||||
|
// 'useShortDescription: false' sorgt dafür, dass die vollständige, lange Beschreibung verwendet wird.
|
||||||
return ServiceResult.Ok(MapToDto(product, useShortDescription: false));
|
return ServiceResult.Ok(MapToDto(product, useShortDescription: false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ruft eine Liste der als "hervorgehoben" markierten Produkte ab (z.B. für die Startseite).
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Eine Liste von ProductDto-Objekten, sortiert nach ihrer Anzeigereihenfolge.</returns>
|
||||||
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetFeaturedProductsAsync()
|
public async Task<ServiceResult<IEnumerable<ProductDto>>> GetFeaturedProductsAsync()
|
||||||
{
|
{
|
||||||
|
// Startet eine Abfrage auf die 'Products'-Tabelle.
|
||||||
var products = await _context.Products
|
var products = await _context.Products
|
||||||
|
// Filtert nur Produkte, die sowohl aktiv als auch als "featured" markiert sind.
|
||||||
.Where(p => p.IsActive && p.IsFeatured)
|
.Where(p => p.IsActive && p.IsFeatured)
|
||||||
|
// Sortiert die Ergebnisse nach der vom Admin festgelegten Reihenfolge.
|
||||||
.OrderBy(p => p.FeaturedDisplayOrder)
|
.OrderBy(p => p.FeaturedDisplayOrder)
|
||||||
|
// Lädt die zugehörigen Bilder und Kategorien.
|
||||||
.Include(p => p.Images)
|
.Include(p => p.Images)
|
||||||
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
.Include(p => p.Productcategories).ThenInclude(pc => pc.categorie)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
|
|
||||||
|
// Wandelt die Entitäten in DTOs um (mit kurzer Beschreibung).
|
||||||
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
var dtos = products.Select(p => MapToDto(p, useShortDescription: true)).ToList();
|
||||||
|
|
||||||
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
return ServiceResult.Ok<IEnumerable<ProductDto>>(dtos);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private Hilfsmethode, um Codeduplizierung zu vermeiden
|
/// <summary>
|
||||||
|
/// Private Hilfsmethode, um eine Product-Entität in ein ProductDto zu konvertieren.
|
||||||
|
/// Dies vermeidet Codeduplizierung und stellt eine konsistente Datenstruktur sicher.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="product">Die Product-Entität aus der Datenbank.</param>
|
||||||
|
/// <param name="useShortDescription">Gibt an, ob die Kurz- oder Langbeschreibung verwendet werden soll.</param>
|
||||||
|
/// <returns>Ein befülltes ProductDto-Objekt.</returns>
|
||||||
private ProductDto MapToDto(Product product, bool useShortDescription)
|
private ProductDto MapToDto(Product product, bool useShortDescription)
|
||||||
{
|
{
|
||||||
return new ProductDto
|
return new ProductDto
|
||||||
{
|
{
|
||||||
Id = product.Id,
|
Id = product.Id,
|
||||||
Name = product.Name,
|
Name = product.Name,
|
||||||
|
// Wählt die passende Beschreibung basierend auf dem 'useShortDescription'-Flag.
|
||||||
Description = useShortDescription ? product.ShortDescription : product.Description,
|
Description = useShortDescription ? product.ShortDescription : product.Description,
|
||||||
Price = product.Price,
|
Price = product.Price,
|
||||||
OldPrice = product.OldPrice,
|
OldPrice = product.OldPrice,
|
||||||
@@ -75,12 +123,14 @@ namespace Webshop.Application.Services.Public
|
|||||||
IsInStock = product.IsInStock,
|
IsInStock = product.IsInStock,
|
||||||
StockQuantity = product.StockQuantity,
|
StockQuantity = product.StockQuantity,
|
||||||
Slug = product.Slug,
|
Slug = product.Slug,
|
||||||
|
// Mappt die verknüpften Kategorien auf eine Liste von CategorieDto.
|
||||||
categories = product.Productcategories.Select(pc => new CategorieDto
|
categories = product.Productcategories.Select(pc => new CategorieDto
|
||||||
{
|
{
|
||||||
Id = pc.categorie.Id,
|
Id = pc.categorie.Id,
|
||||||
Name = pc.categorie.Name,
|
Name = pc.categorie.Name,
|
||||||
Slug = pc.categorie.Slug
|
Slug = pc.categorie.Slug
|
||||||
}).ToList(),
|
}).ToList(),
|
||||||
|
// Mappt die verknüpften Bilder, sortiert nach DisplayOrder, auf eine Liste von ProductImageDto.
|
||||||
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
Images = product.Images.OrderBy(i => i.DisplayOrder).Select(img => new ProductImageDto
|
||||||
{
|
{
|
||||||
Id = img.Id,
|
Id = img.Id,
|
||||||
|
|||||||
Reference in New Issue
Block a user