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

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