91 lines
3.8 KiB
C#
91 lines
3.8 KiB
C#
// src/Webshop.Application/Services/ProductService.cs
|
|
using Webshop.Application.DTOs;
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Interfaces;
|
|
|
|
namespace Webshop.Application.Services
|
|
{
|
|
public class ProductService
|
|
{
|
|
private readonly IProductRepository _productRepository;
|
|
|
|
public ProductService(IProductRepository productRepository)
|
|
{
|
|
_productRepository = productRepository;
|
|
}
|
|
|
|
public async Task<IEnumerable<ProductDto>> GetAllProductsAsync()
|
|
{
|
|
var productsFromDb = await _productRepository.GetAllProductsAsync();
|
|
|
|
var productDtos = productsFromDb.Select(p => new ProductDto
|
|
{
|
|
Id = p.Id,
|
|
Name = p.Name,
|
|
Description = p.Description,
|
|
Price = p.Price,
|
|
Sku = p.SKU
|
|
});
|
|
|
|
return productDtos;
|
|
}
|
|
|
|
public async Task<ProductDto> CreateProductAsync(ProductDto productDto)
|
|
{
|
|
var newProduct = new Product
|
|
{
|
|
// Felder aus DTO
|
|
Name = productDto.Name,
|
|
Description = productDto.Description,
|
|
Price = productDto.Price,
|
|
SKU = productDto.Sku,
|
|
ShortDescription = productDto.ShortDescription,
|
|
IsActive = productDto.IsActive,
|
|
IsInStock = productDto.IsInStock,
|
|
StockQuantity = productDto.StockQuantity,
|
|
Slug = productDto.Slug, // WICHTIG: Hier muss ein Wert übergeben werden
|
|
// Der Slug sollte am besten automatisch generiert werden,
|
|
// z.B. aus dem Produktnamen, um Eindeutigkeit zu gewährleisten.
|
|
|
|
// Felder, die im Backend initialisiert werden
|
|
Id = Guid.NewGuid(), // Normalerweise wird die ID hier oder in der DB generiert
|
|
CreatedDate = DateTimeOffset.UtcNow, // WICHTIG: Erstellungsdatum setzen
|
|
LastModifiedDate = null, // Beim Erstellen null
|
|
// Andere optionale Felder, die du vielleicht vom Client nicht erwartest, aber zuweisen musst:
|
|
// OldPrice = null,
|
|
// Weight = null,
|
|
// ...
|
|
};
|
|
|
|
// Optional: Logik zur Generierung des Slugs, falls er nicht vom Client kommt
|
|
if (string.IsNullOrWhiteSpace(newProduct.Slug) && !string.IsNullOrWhiteSpace(newProduct.Name))
|
|
{
|
|
newProduct.Slug = GenerateSlug(newProduct.Name); // Eigene Implementierung für Slug-Generierung
|
|
}
|
|
// Optional: Wenn Slug UNIQUE sein muss, hier auf Eindeutigkeit prüfen und ggf. anpassen
|
|
// await _productRepository.EnsureUniqueSlug(newProduct.Slug); // Beispiel
|
|
// Wenn der Slug UNIQUE sein soll, solltest du die Überprüfung und ggf. Regenerierung hier im Service vornehmen.
|
|
|
|
await _productRepository.AddProductAsync(newProduct);
|
|
|
|
// Aktualisiere das DTO mit der generierten ID und anderen Backend-generierten Werten
|
|
productDto.Id = newProduct.Id;
|
|
// Wenn du den Slug zurückgeben willst, der generiert wurde:
|
|
// productDto.Slug = newProduct.Slug;
|
|
return productDto;
|
|
}
|
|
|
|
private string GenerateSlug(string name)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(name)) return Guid.NewGuid().ToString(); // Fallback
|
|
var slug = name.ToLowerInvariant()
|
|
.Replace(" ", "-")
|
|
.Replace("ä", "ae").Replace("ö", "oe").Replace("ü", "ue")
|
|
.Replace("ß", "ss")
|
|
.Trim();
|
|
// Entferne ungültige Zeichen
|
|
slug = System.Text.RegularExpressions.Regex.Replace(slug, @"[^a-z0-9-]", "");
|
|
return slug;
|
|
}
|
|
}
|
|
} |