checkout
This commit is contained in:
38
Webshop.Application/Services/Customers/CartService.cs
Normal file
38
Webshop.Application/Services/Customers/CartService.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user