diff --git a/Webshop.Application/DTOs/Products/ProductDto.cs b/Webshop.Application/DTOs/Products/ProductDto.cs index 123837c..82af6e2 100644 --- a/Webshop.Application/DTOs/Products/ProductDto.cs +++ b/Webshop.Application/DTOs/Products/ProductDto.cs @@ -17,5 +17,6 @@ namespace Webshop.Application.DTOs.Products public bool IsInStock { get; set; } public int StockQuantity { get; set; } public string? ImageUrl { get; set; } + public string Slug { get; set; } = string.Empty; } } diff --git a/Webshop.Application/Services/Public/ProductService.cs b/Webshop.Application/Services/Public/ProductService.cs index 2c9182a..aa27961 100644 --- a/Webshop.Application/Services/Public/ProductService.cs +++ b/Webshop.Application/Services/Public/ProductService.cs @@ -1,12 +1,9 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; -using Webshop.Domain.Interfaces; -using Webshop.Domain.Entities; using Webshop.Application.DTOs.Products; using Webshop.Application.Services.Public.Interfaces; - +using Webshop.Domain.Interfaces; namespace Webshop.Application.Services.Public { @@ -19,9 +16,47 @@ namespace Webshop.Application.Services.Public _productRepository = productRepository; } - // HIER KOMMT DER VORHERIGE PRODUCTSERVICE CODE HIN (GetAllProductsAsync, CreateProductAsync etc.) - // Hier sind Platzhalter-Implementierungen, die Sie durch den vollständigen Code ersetzen müssen: - public async Task> GetAllProductsAsync() { return new List(); } - public async Task CreateProductAsync(ProductDto productDto) { return null; } + // Wir verwenden jetzt den von Ihnen vorgegebenen Namen + public async Task> GetAllProductsAsync() + { + var products = await _productRepository.GetAllProductsAsync(); + + return products + .Where(p => p.IsActive) // Nur aktive Produkte anzeigen + .Select(p => new ProductDto + { + Id = p.Id, + Name = p.Name, + Description = p.ShortDescription, + Price = p.Price, + SKU = p.SKU, + ImageUrl = p.ImageUrl, + IsInStock = p.IsInStock, + Slug = p.Slug // Mapping für Slug + }).ToList(); + } + + // Diese Methode wird hinzugefügt, um das Interface zu erfüllen + public async Task GetProductBySlugAsync(string slug) + { + var product = await _productRepository.GetBySlugAsync(slug); + + if (product == null) + { + return null; + } + + return new ProductDto + { + Id = product.Id, + Name = product.Name, + Description = product.Description, + Price = product.Price, + SKU = product.SKU, + ImageUrl = product.ImageUrl, + IsInStock = product.IsInStock, + Slug = product.Slug // Mapping für Slug + }; + } } -} +} \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/IProductRepository.cs b/Webshop.Domain/Interfaces/IProductRepository.cs index b6a8e5b..9b0a0c7 100644 --- a/Webshop.Domain/Interfaces/IProductRepository.cs +++ b/Webshop.Domain/Interfaces/IProductRepository.cs @@ -1,14 +1,16 @@ -// src/Webshop.Domain/Interfaces/IProductRepository.cs +using System; using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Domain.Entities; // Product Entity +using Webshop.Domain.Entities; namespace Webshop.Domain.Interfaces { public interface IProductRepository { Task> GetAllProductsAsync(); + Task GetProductByIdAsync(Guid id); + Task GetBySlugAsync(string slug); Task AddProductAsync(Product product); Task UpdateProductAsync(Product product); Task DeleteProductAsync(Guid id); diff --git a/Webshop.Infrastructure/Repositories/ProductRepository.cs b/Webshop.Infrastructure/Repositories/ProductRepository.cs index 4985af2..4fc5708 100644 --- a/Webshop.Infrastructure/Repositories/ProductRepository.cs +++ b/Webshop.Infrastructure/Repositories/ProductRepository.cs @@ -1,15 +1,14 @@ -// src/Webshop.Infrastructure/Repositories/ProductRepository.cs -using Microsoft.EntityFrameworkCore; // Wichtig für ToListAsync, FindAsync etc. +using Microsoft.EntityFrameworkCore; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; using Webshop.Infrastructure.Data; -using System; // Für Guid +using System; using System.Collections.Generic; using System.Threading.Tasks; namespace Webshop.Infrastructure.Repositories { - public class ProductRepository : IProductRepository // ACHTUNG: Hier muss das Interface stehen + public class ProductRepository : IProductRepository { private readonly ApplicationDbContext _context; @@ -18,8 +17,6 @@ namespace Webshop.Infrastructure.Repositories _context = context; } - // --- IMPLEMENTIERUNG DER IProductRepository METHODEN --- - public async Task> GetAllProductsAsync() { return await _context.Products.ToListAsync(); @@ -30,10 +27,17 @@ namespace Webshop.Infrastructure.Repositories return await _context.Products.FindAsync(id); } + // --- HIER DIE NEUE METHODE IMPLEMENTIEREN --- + public async Task GetBySlugAsync(string slug) + { + // Sucht nur nach aktiven Produkten, was für die öffentliche Ansicht korrekt ist. + return await _context.Products.FirstOrDefaultAsync(p => p.Slug == slug && p.IsActive); + } + public async Task AddProductAsync(Product product) { - _context.Products.Add(product); - await _context.SaveChangesAsync(); // Änderungen in der DB speichern + await _context.Products.AddAsync(product); + await _context.SaveChangesAsync(); } public async Task UpdateProductAsync(Product product) @@ -44,11 +48,11 @@ namespace Webshop.Infrastructure.Repositories public async Task DeleteProductAsync(Guid id) { - var product = await _context.Products.FindAsync(id); // Produkt zuerst finden + var product = await GetProductByIdAsync(id); if (product != null) { - _context.Products.Remove(product); // Produkt entfernen - await _context.SaveChangesAsync(); // Änderungen speichern + _context.Products.Remove(product); + await _context.SaveChangesAsync(); } } }