This commit is contained in:
Tizian.Breuch
2025-07-25 16:23:16 +02:00
parent f318340f98
commit 2d6eeb7a50
4 changed files with 65 additions and 23 deletions

View File

@@ -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;
}
}

View File

@@ -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<IEnumerable<ProductDto>> GetAllProductsAsync() { return new List<ProductDto>(); }
public async Task<ProductDto> CreateProductAsync(ProductDto productDto) { return null; }
// Wir verwenden jetzt den von Ihnen vorgegebenen Namen
public async Task<IEnumerable<ProductDto>> 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<ProductDto?> 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
};
}
}
}

View File

@@ -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<IEnumerable<Product>> GetAllProductsAsync();
Task<Product?> GetProductByIdAsync(Guid id);
Task<Product?> GetBySlugAsync(string slug);
Task AddProductAsync(Product product);
Task UpdateProductAsync(Product product);
Task DeleteProductAsync(Guid id);

View File

@@ -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<IEnumerable<Product>> 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<Product?> 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();
}
}
}