cart
Some checks failed
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Failing after 20s

This commit is contained in:
Tizian.Breuch
2025-11-26 17:21:01 +01:00
parent c4d4a27798
commit 9c48f62ff0
4 changed files with 148 additions and 11 deletions

View File

@@ -1,7 +1,12 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Customers;
using Webshop.Application.DTOs.Shipping;
using Webshop.Application.Services.Customers.Interfaces;
using Webshop.Domain.Entities;
using Webshop.Infrastructure.Data;
namespace Webshop.Application.Services.Customers
@@ -15,24 +20,87 @@ namespace Webshop.Application.Services.Customers
_context = context;
}
public async Task<ServiceResult<CartDto>> GetCartAsync(string userId)
{
var cart = await GetCartEntity(userId);
if (cart == null) return ServiceResult.Ok(new CartDto()); // Leerer Warenkorb
var dto = new CartDto
{
Id = cart.Id,
UserId = userId,
Items = cart.Items.Select(i => new CartItemDto
{
ProductId = i.ProductId,
ProductVariantId = i.ProductVariantId,
Quantity = i.Quantity
}).ToList()
};
return ServiceResult.Ok(dto);
}
public async Task<ServiceResult> AddToCartAsync(string userId, CartItemDto itemDto)
{
var cart = await GetCartEntity(userId);
if (cart == null)
{
cart = new Cart { UserId = userId, Id = Guid.NewGuid() };
_context.Carts.Add(cart);
}
var existingItem = cart.Items.FirstOrDefault(i => i.ProductId == itemDto.ProductId && i.ProductVariantId == itemDto.ProductVariantId);
if (existingItem != null)
{
existingItem.Quantity += itemDto.Quantity;
}
else
{
var newItem = new CartItem
{
CartId = cart.Id,
ProductId = itemDto.ProductId,
ProductVariantId = itemDto.ProductVariantId,
Quantity = itemDto.Quantity
};
cart.Items.Add(newItem); // Wichtig: Muss in CartItems DbSet landen
_context.CartItems.Add(newItem);
}
cart.LastModified = DateTime.UtcNow;
await _context.SaveChangesAsync();
return ServiceResult.Ok();
}
public async Task<ServiceResult> RemoveFromCartAsync(string userId, Guid productId)
{
var cart = await GetCartEntity(userId);
if (cart == null) return ServiceResult.Fail(ServiceResultType.NotFound, "Warenkorb leer.");
var item = cart.Items.FirstOrDefault(i => i.ProductId == productId);
if (item != null)
{
_context.CartItems.Remove(item);
await _context.SaveChangesAsync();
}
return ServiceResult.Ok();
}
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);
var cart = await GetCartEntity(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();
}
}
private async Task<Cart?> GetCartEntity(string userId)
{
return await _context.Carts
.Include(c => c.Items)
.FirstOrDefaultAsync(c => c.UserId == userId);
}
}
}

View File

@@ -1,9 +1,14 @@
using System.Threading.Tasks;
using Webshop.Application.DTOs.Shipping; // Für CartItemDto
using Webshop.Application; // Für ServiceResult
namespace Webshop.Application.Services.Customers.Interfaces
{
public interface ICartService
{
Task<ServiceResult<CartDto>> GetCartAsync(string userId);
Task<ServiceResult> AddToCartAsync(string userId, CartItemDto item);
Task<ServiceResult> RemoveFromCartAsync(string userId, Guid productId);
Task ClearCartAsync(string userId);
}
}