Projektdateien hinzufügen.
This commit is contained in:
51
Webshop.Infrastructure/Repositories/ProductRepository.cs
Normal file
51
Webshop.Infrastructure/Repositories/ProductRepository.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
// src/Webshop.Infrastructure/Repositories/ProductRepository.cs
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Webshop.Domain.Entities;
|
||||
using Webshop.Domain.Interfaces;
|
||||
using Webshop.Infrastructure.Data;
|
||||
|
||||
namespace Webshop.Infrastructure.Repositories
|
||||
{
|
||||
public class ProductRepository : IProductRepository
|
||||
{
|
||||
private readonly ApplicationDbContext _context;
|
||||
|
||||
// Wir lassen uns den DbContext per Dependency Injection geben
|
||||
public ProductRepository(ApplicationDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
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(); // Wichtig: Änderungen 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);
|
||||
if (product != null)
|
||||
{
|
||||
_context.Products.Remove(product);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user