From de67e01f2c7a9275c296dfee777365eb999b53d1 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Wed, 26 Nov 2025 17:31:28 +0100 Subject: [PATCH] fix --- .../Controllers/Customers/CartController.cs | 12 ++++++++---- Webshop.Application/DTOs/Customers/CartDto.cs | 6 ++++-- .../Services/Customers/CartService.cs | 16 ++++++++++++---- .../Customers/Interfaces/ICartService.cs | 6 ++++-- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Webshop.Api/Controllers/Customers/CartController.cs b/Webshop.Api/Controllers/Customers/CartController.cs index cdd045b..1c2314a 100644 --- a/Webshop.Api/Controllers/Customers/CartController.cs +++ b/Webshop.Api/Controllers/Customers/CartController.cs @@ -5,8 +5,8 @@ using System; using System.Security.Claims; using System.Threading.Tasks; using Webshop.Application; -using Webshop.Application.DTOs.Customers; -using Webshop.Application.DTOs.Shipping; +using Webshop.Application.DTOs.Customers; // Für CartDto +using Webshop.Application.DTOs.Shipping; // Für CartItemDto using Webshop.Application.Services.Customers.Interfaces; namespace Webshop.Api.Controllers.Customer @@ -14,7 +14,7 @@ namespace Webshop.Api.Controllers.Customer [ApiController] [Route("api/v1/customer/[controller]")] [Authorize(Roles = "Customer")] - public class CartController : ControllerBase + public class CartController : ControllerBase // <--- WICHTIG: Muss public sein und erben { private readonly ICartService _cartService; @@ -24,6 +24,7 @@ namespace Webshop.Api.Controllers.Customer } [HttpGet] + [ProducesResponseType(typeof(CartDto), StatusCodes.Status200OK)] public async Task GetCart() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); @@ -32,14 +33,17 @@ namespace Webshop.Api.Controllers.Customer } [HttpPost("items")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)] public async Task AddToCart([FromBody] CartItemDto item) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var result = await _cartService.AddToCartAsync(userId!, item); - return result.Type == ServiceResultType.Success ? Ok() : BadRequest(result.ErrorMessage); + return result.Type == ServiceResultType.Success ? Ok() : BadRequest(new { Message = result.ErrorMessage }); } [HttpDelete("items/{productId}")] + [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task RemoveItem(Guid productId) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); diff --git a/Webshop.Application/DTOs/Customers/CartDto.cs b/Webshop.Application/DTOs/Customers/CartDto.cs index 9b8d492..f29a03c 100644 --- a/Webshop.Application/DTOs/Customers/CartDto.cs +++ b/Webshop.Application/DTOs/Customers/CartDto.cs @@ -1,6 +1,6 @@ using System; using System.Collections.Generic; -using Webshop.Application.DTOs.Shipping; // Oder wo CartItemDto liegt +using Webshop.Application.DTOs.Shipping; // Wichtig für CartItemDto namespace Webshop.Application.DTOs.Customers { @@ -9,6 +9,8 @@ namespace Webshop.Application.DTOs.Customers public Guid Id { get; set; } public string UserId { get; set; } = string.Empty; public List Items { get; set; } = new List(); - public decimal TotalEstimatedPrice { get; set; } // Optional: Summe berechnet + + // Optional: Gesamtsumme zur Anzeige im Frontend + public decimal TotalPrice { get; set; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Customers/CartService.cs b/Webshop.Application/Services/Customers/CartService.cs index 0958bf6..c9cfb5e 100644 --- a/Webshop.Application/Services/Customers/CartService.cs +++ b/Webshop.Application/Services/Customers/CartService.cs @@ -3,8 +3,8 @@ 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.DTOs.Customers; // <--- DIESE ZEILE HAT GEFEHLT (für CartDto) +using Webshop.Application.DTOs.Shipping; // (für CartItemDto) using Webshop.Application.Services.Customers.Interfaces; using Webshop.Domain.Entities; using Webshop.Infrastructure.Data; @@ -20,10 +20,16 @@ namespace Webshop.Application.Services.Customers _context = context; } + // Die Implementierung muss exakt so aussehen: public async Task> GetCartAsync(string userId) { var cart = await GetCartEntity(userId); - if (cart == null) return ServiceResult.Ok(new CartDto()); // Leerer Warenkorb + + // Wenn kein Warenkorb da ist, geben wir einen leeren zurück (Success) + if (cart == null) + { + return ServiceResult.Ok(new CartDto { UserId = userId }); + } var dto = new CartDto { @@ -36,6 +42,7 @@ namespace Webshop.Application.Services.Customers Quantity = i.Quantity }).ToList() }; + return ServiceResult.Ok(dto); } @@ -63,7 +70,8 @@ namespace Webshop.Application.Services.Customers ProductVariantId = itemDto.ProductVariantId, Quantity = itemDto.Quantity }; - cart.Items.Add(newItem); // Wichtig: Muss in CartItems DbSet landen + // WICHTIG: Zur Collection hinzufügen UND dem Context Bescheid geben + cart.Items.Add(newItem); _context.CartItems.Add(newItem); } diff --git a/Webshop.Application/Services/Customers/Interfaces/ICartService.cs b/Webshop.Application/Services/Customers/Interfaces/ICartService.cs index 12eec4b..ddc32d6 100644 --- a/Webshop.Application/Services/Customers/Interfaces/ICartService.cs +++ b/Webshop.Application/Services/Customers/Interfaces/ICartService.cs @@ -1,6 +1,8 @@ using System.Threading.Tasks; -using Webshop.Application.DTOs.Shipping; // Für CartItemDto -using Webshop.Application; // Für ServiceResult +using System; // Für Guid +using Webshop.Application.DTOs.Customers; // <--- WICHTIG für CartDto +using Webshop.Application.DTOs.Shipping; +using Webshop.Application; namespace Webshop.Application.Services.Customers.Interfaces {