All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 25s
64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Webshop.Domain.Entities;
|
|
using Webshop.Domain.Interfaces;
|
|
using Webshop.Infrastructure.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Webshop.Infrastructure.Repositories
|
|
{
|
|
public class ProductRepository : IProductRepository
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
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);
|
|
}
|
|
|
|
// --- 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)
|
|
{
|
|
await _context.Products.AddAsync(product);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateProductAsync(Product product)
|
|
{
|
|
_context.Products.Update(product);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task DeleteProductAsync(Guid id)
|
|
{
|
|
var product = await GetProductByIdAsync(id);
|
|
if (product != null)
|
|
{
|
|
_context.Products.Remove(product);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task SaveChangesAsync()
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
} |