56 lines
1.6 KiB
C#
56 lines
1.6 KiB
C#
// 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)
|
|
{
|
|
await _context.Products.AddAsync(product);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task<bool> ProductExistsBySlugAsync(string slug)
|
|
{
|
|
return await _context.Products.AnyAsync(p => p.Slug == slug);
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
} |