using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using System; using System.Security.Claims; using System.Threading.Tasks; using Webshop.Application; 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 { [ApiController] [Route("api/v1/customer/[controller]")] [Authorize(Roles = "Customer")] public class CartController : ControllerBase // <--- WICHTIG: Muss public sein und erben { private readonly ICartService _cartService; public CartController(ICartService cartService) { _cartService = cartService; } [HttpGet] [ProducesResponseType(typeof(CartDto), StatusCodes.Status200OK)] public async Task GetCart() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var result = await _cartService.GetCartAsync(userId!); return Ok(result.Value); } [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(new { Message = result.ErrorMessage }); } [HttpDelete("items/{productId}")] [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task RemoveItem(Guid productId) { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); await _cartService.RemoveFromCartAsync(userId!, productId); return NoContent(); } } }