74 lines
3.3 KiB
C#
74 lines
3.3 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;
|
|
|
|
namespace Webshop.Api.Controllers.Customer
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/customer/[controller]")]
|
|
[Authorize(Roles = "Customer")]
|
|
public class CheckoutController : ControllerBase
|
|
{
|
|
private readonly ICheckoutService _checkoutService;
|
|
|
|
public CheckoutController(ICheckoutService checkoutService)
|
|
{
|
|
_checkoutService = checkoutService;
|
|
}
|
|
|
|
// --- NEU: Endpoint um verfügbare Versandmethoden basierend auf dem Warenkorb zu holen ---
|
|
[HttpPost("available-shipping-methods")]
|
|
[ProducesResponseType(typeof(IEnumerable<ShippingMethodDto>), StatusCodes.Status200OK)]
|
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
|
public async Task<IActionResult> GetAvailableShippingMethods([FromBody] ShippingCalculationRequestDto request)
|
|
{
|
|
var result = await _checkoutService.GetCompatibleShippingMethodsAsync(request.Items);
|
|
|
|
return result.Type switch
|
|
{
|
|
ServiceResultType.Success => Ok(result.Value),
|
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = "Fehler beim Laden der Versandmethoden." })
|
|
};
|
|
}
|
|
|
|
[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." })
|
|
};
|
|
}
|
|
}
|
|
} |