try
This commit is contained in:
@@ -1,21 +1,25 @@
|
||||
// src/Webshop.Infrastructure/Repositories/ProductRepository.cs
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// src/Webshop.Infrastructure/Repositories/ProductRepository.cs
|
||||
using Microsoft.EntityFrameworkCore; // Wichtig für ToListAsync, FindAsync etc.
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Infrastructure.Data;
|
||||
using System; // Für Guid
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Webshop.Infrastructure.Repositories
|
||||
{
|
||||
public class ProductRepository : IProductRepository
|
||||
public class ProductRepository : IProductRepository // ACHTUNG: Hier muss das Interface stehen
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
// Wir lassen uns den DbContext per Dependency Injection geben
|
||||
public ProductRepository(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
// --- IMPLEMENTIERUNG DER IProductRepository METHODEN ---
|
||||
|
||||
public async Task<IEnumerable<Product>> GetAllProductsAsync()
|
||||
{
|
||||
return await _context.Products.ToListAsync();
|
||||
@@ -28,13 +32,8 @@ namespace Webshop.Infrastructure.Repositories
|
||||
|
||||
public async Task AddProductAsync(Product product)
|
||||
{
|
||||
await _context.Products.AddAsync(product);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> ProductExistsBySlugAsync(string slug)
|
||||
{
|
||||
return await _context.Products.AnyAsync(p => p.Slug == slug);
|
||||
_context.Products.Add(product);
|
||||
await _context.SaveChangesAsync(); // Änderungen in der DB speichern
|
||||
}
|
||||
|
||||
public async Task UpdateProductAsync(Product product)
|
||||
@@ -45,11 +44,11 @@ namespace Webshop.Infrastructure.Repositories
|
||||
|
||||
public async Task DeleteProductAsync(Guid id)
|
||||
{
|
||||
var product = await _context.Products.FindAsync(id);
|
||||
var product = await _context.Products.FindAsync(id); // Produkt zuerst finden
|
||||
if (product != null)
|
||||
{
|
||||
_context.Products.Remove(product);
|
||||
await _context.SaveChangesAsync();
|
||||
_context.Products.Remove(product); // Produkt entfernen
|
||||
await _context.SaveChangesAsync(); // Änderungen speichern
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user