dto change
This commit is contained in:
@@ -3,10 +3,16 @@ namespace Webshop.Application.DTOs
|
|||||||
{
|
{
|
||||||
public class ProductDto
|
public class ProductDto
|
||||||
{
|
{
|
||||||
public Guid Id { get; set; }
|
public Guid Id { get; set; } // Wird bei Erstellung oft vom Backend gesetzt
|
||||||
public string Name { get; set; } = string.Empty;
|
public string Name { get; set; } = string.Empty;
|
||||||
public string Description { get; set; } = string.Empty;
|
public string Description { get; set; } = string.Empty; // Kann null sein
|
||||||
public decimal Price { get; set; }
|
public decimal Price { get; set; }
|
||||||
public string Sku { get; set; } = string.Empty;
|
public string Sku { get; set; } = string.Empty;
|
||||||
|
public string ShortDescription { get; set; } = string.Empty; // Kann null sein, aber hier zur Vollständigkeit
|
||||||
|
public bool IsActive { get; set; } // Muss übergeben werden
|
||||||
|
public bool IsInStock { get; set; } // Muss übergeben werden
|
||||||
|
public int StockQuantity { get; set; } // Muss übergeben werden
|
||||||
|
public string Slug { get; set; } = string.Empty; // Muss übergeben werden
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
// src/Webshop.Application/Services/ProductService.cs
|
// src/Webshop.Application/Services/ProductService.cs
|
||||||
using Webshop.Application.DTOs;
|
using Webshop.Application.DTOs;
|
||||||
|
using Webshop.Domain.Entities;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
namespace Webshop.Application.Services
|
namespace Webshop.Application.Services
|
||||||
@@ -31,18 +32,60 @@ namespace Webshop.Application.Services
|
|||||||
|
|
||||||
public async Task<ProductDto> CreateProductAsync(ProductDto productDto)
|
public async Task<ProductDto> CreateProductAsync(ProductDto productDto)
|
||||||
{
|
{
|
||||||
var newProduct = new Domain.Entities.Product
|
var newProduct = new Product
|
||||||
{
|
{
|
||||||
|
// Felder aus DTO
|
||||||
Name = productDto.Name,
|
Name = productDto.Name,
|
||||||
Description = productDto.Description,
|
Description = productDto.Description,
|
||||||
Price = productDto.Price,
|
Price = productDto.Price,
|
||||||
SKU = productDto.Sku
|
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);
|
await _productRepository.AddProductAsync(newProduct);
|
||||||
|
|
||||||
|
// Aktualisiere das DTO mit der generierten ID und anderen Backend-generierten Werten
|
||||||
productDto.Id = newProduct.Id;
|
productDto.Id = newProduct.Id;
|
||||||
|
// Wenn du den Slug zurückgeben willst, der generiert wurde:
|
||||||
|
// productDto.Slug = newProduct.Slug;
|
||||||
return productDto;
|
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