// src/Webshop.Api/Controllers/Customer/CheckoutController.cs using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Authorization; using System.Threading.Tasks; using Webshop.Application.Services.Customers.Interfaces; using Webshop.Application.DTOs.Orders; using Webshop.Application.DTOs.Shipping; // Neu using System.Security.Claims; using Microsoft.AspNetCore.Http; using Webshop.Application; using System.Collections.Generic; using Webshop.Application.Services.Customers; using Microsoft.AspNetCore.Cors.Infrastructure; namespace Webshop.Api.Controllers.Customer { [ApiController] [Route("api/v1/customer/[controller]")] [Authorize(Roles = "Customer")] public class CheckoutController : ControllerBase { private readonly ICheckoutService _checkoutService; private readonly ICartService _cartService; public CheckoutController(ICheckoutService checkoutService , ICartService cartService) { _checkoutService = checkoutService; _cartService = cartService; } [HttpGet("available-shipping-methods")] // War vorher POST [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] public async Task GetAvailableShippingMethods() { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); // 1. Warenkorb laden var cartResult = await _cartService.GetCartAsync(userId!); if (cartResult.Value == null || !cartResult.Value.Items.Any()) { return Ok(new List()); // Leerer Korb -> keine Methoden } // 2. Berechnung aufrufen (nutzt die Overload Methode mit List) var result = await _checkoutService.GetCompatibleShippingMethodsAsync(cartResult.Value.Items); return result.Type switch { ServiceResultType.Success => Ok(result.Value), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = "Fehler." }) }; } [HttpPost("create-order")] [ProducesResponseType(typeof(OrderDetailDto), StatusCodes.Status201Created)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status401Unauthorized)] [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status409Conflict)] public async Task CreateOrder([FromBody] CreateOrderDto orderDto) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(userId)) { return Unauthorized(new { Message = "Benutzer konnte nicht identifiziert werden." }); } var result = await _checkoutService.CreateOrderAsync(orderDto, userId); return result.Type switch { ServiceResultType.Success => Created($"/api/v1/customer/orders/{result.Value!.Id}", result.Value), ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }), ServiceResultType.Conflict => Conflict(new { Message = result.ErrorMessage }), ServiceResultType.Unauthorized => Unauthorized(new { Message = result.ErrorMessage }), ServiceResultType.Forbidden => Forbid(), _ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." }) }; } } }