Files
ShopSolution-backend/Webshop.Api/Controllers/Customers/CheckoutController.cs
Webtree-design d48d83c87d
All checks were successful
Branch - test - Build and Push Backend API Docker Image / build-and-push (push) Successful in 29s
direkt aus dem warenkorb holen
2025-11-28 13:12:24 +01:00

85 lines
3.7 KiB
C#

// 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<ShippingMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> 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<ShippingMethodDto>()); // Leerer Korb -> keine Methoden
}
// 2. Berechnung aufrufen (nutzt die Overload Methode mit List<CartItemDto>)
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<IActionResult> 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." })
};
}
}
}