55 lines
1.7 KiB
C#
55 lines
1.7 KiB
C#
// 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 // ACHTUNG: Hier muss das Interface stehen
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public ProductRepository(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
// --- IMPLEMENTIERUNG DER IProductRepository METHODEN ---
|
|
|
|
public async Task<IEnumerable<Product>> GetAllProductsAsync()
|
|
{
|
|
return await _context.Products.ToListAsync();
|
|
}
|
|
|
|
public async Task<Product?> GetProductByIdAsync(Guid id)
|
|
{
|
|
return await _context.Products.FindAsync(id);
|
|
}
|
|
|
|
public async Task AddProductAsync(Product product)
|
|
{
|
|
_context.Products.Add(product);
|
|
await _context.SaveChangesAsync(); // Änderungen in der DB speichern
|
|
}
|
|
|
|
public async Task UpdateProductAsync(Product product)
|
|
{
|
|
_context.Products.Update(product);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteProductAsync(Guid id)
|
|
{
|
|
var product = await _context.Products.FindAsync(id); // Produkt zuerst finden
|
|
if (product != null)
|
|
{
|
|
_context.Products.Remove(product); // Produkt entfernen
|
|
await _context.SaveChangesAsync(); // Änderungen speichern
|
|
}
|
|
}
|
|
}
|
|
} |