38 lines
1.2 KiB
C#
38 lines
1.2 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application.Services.Customers.Interfaces;
|
|
using Webshop.Infrastructure.Data;
|
|
|
|
namespace Webshop.Application.Services.Customers
|
|
{
|
|
public class CartService : ICartService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public CartService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task ClearCartAsync(string userId)
|
|
{
|
|
// Wir suchen den Warenkorb des Users
|
|
// Annahme: Es gibt eine 'Carts' Tabelle und 'CartItems'
|
|
var cart = await _context.Carts
|
|
.Include(c => c.Items)
|
|
.FirstOrDefaultAsync(c => c.UserId == userId);
|
|
|
|
if (cart != null && cart.Items.Any())
|
|
{
|
|
// Option A: Nur Items löschen, Warenkorb behalten
|
|
_context.CartItems.RemoveRange(cart.Items);
|
|
|
|
// Option B: Ganzen Warenkorb löschen (dann wird er beim nächsten AddToCart neu erstellt)
|
|
// _context.Carts.Remove(cart);
|
|
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
} |