bugs
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
// src/Webshop.Application/Services/ProductService.cs
|
||||
using Webshop.Application.DTOs;
|
||||
using Webshop.Domain.Entities;
|
||||
// src/Webshop.Application/Services/Public/ProductService.cs
|
||||
using Webshop.Application.DTOs; // ProductDto
|
||||
using Webshop.Domain.Interfaces;
|
||||
using System.Collections.Generic; // Sicherstellen, dass für IEnumerable vorhanden
|
||||
using Webshop.Domain.Entities;
|
||||
|
||||
namespace Webshop.Application.Services.Public
|
||||
{
|
||||
public class ProductService
|
||||
public class ProductService // Sie haben den Namen "ProductService" beibehalten
|
||||
{
|
||||
private readonly IProductRepository _productRepository;
|
||||
|
||||
@@ -18,62 +19,40 @@ namespace Webshop.Application.Services.Public
|
||||
{
|
||||
var productsFromDb = await _productRepository.GetAllProductsAsync();
|
||||
|
||||
var productDtos = productsFromDb.Select(p => new ProductDto
|
||||
return productsFromDb.Select(p => new ProductDto
|
||||
{
|
||||
Id = p.Id,
|
||||
Name = p.Name,
|
||||
Description = p.Description,
|
||||
Price = p.Price,
|
||||
Sku = p.SKU
|
||||
});
|
||||
|
||||
return productDtos;
|
||||
SKU = p.SKU,
|
||||
IsActive = p.IsActive,
|
||||
IsInStock = p.IsInStock,
|
||||
StockQuantity = p.StockQuantity,
|
||||
ImageUrl = p.ImageUrl
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
// Beispiel: Methode zum Erstellen eines Produkts (wenn PublicService auch Schreiben erlaubt)
|
||||
// Normalerweise wäre das im AdminProductService
|
||||
public async Task<ProductDto> CreateProductAsync(ProductDto productDto)
|
||||
{
|
||||
var newProduct = new Product
|
||||
{
|
||||
// Felder aus DTO
|
||||
Id = Guid.NewGuid(),
|
||||
Name = productDto.Name,
|
||||
Description = productDto.Description,
|
||||
SKU = productDto.SKU,
|
||||
Price = productDto.Price,
|
||||
SKU = productDto.Sku,
|
||||
ShortDescription = productDto.ShortDescription,
|
||||
IsActive = productDto.IsActive,
|
||||
IsInStock = productDto.IsInStock,
|
||||
IsActive = true, // Annahme
|
||||
IsInStock = true, // Annahme
|
||||
StockQuantity = productDto.StockQuantity,
|
||||
Slug = productDto.Slug,
|
||||
|
||||
|
||||
Id = Guid.NewGuid(),
|
||||
ImageUrl = productDto.ImageUrl,
|
||||
CreatedDate = DateTimeOffset.UtcNow,
|
||||
LastModifiedDate = null,
|
||||
|
||||
};
|
||||
|
||||
if (string.IsNullOrWhiteSpace(newProduct.Slug) && !string.IsNullOrWhiteSpace(newProduct.Name))
|
||||
{
|
||||
newProduct.Slug = GenerateSlug(newProduct.Name);
|
||||
}
|
||||
|
||||
await _productRepository.AddProductAsync(newProduct);
|
||||
|
||||
productDto.Id = newProduct.Id;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user