This commit is contained in:
Tizian.Breuch
2025-09-25 16:23:39 +02:00
parent 90383f2068
commit 1f28c189ce
3 changed files with 55 additions and 161 deletions

View File

@@ -1,13 +1,14 @@
// src/Webshop.Api/Controllers/Customer/OrdersController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Orders;
using Webshop.Application.Services.Customers;
using Webshop.Application.Services.Customers.Interfaces; // Für IOrderService
namespace Webshop.Api.Controllers.Customer
{
@@ -23,49 +24,41 @@ namespace Webshop.Api.Controllers.Customer
_orderService = orderService;
}
[HttpPost]
public async Task<IActionResult> CreateOrder([FromBody] CreateOrderDto orderDto)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId))
{
// In einer [Authorize]-Methode sollte das nie passieren, aber zur Sicherheit
return Unauthorized();
}
var (success, createdOrder, errorMessage) = await _orderService.CreateOrderAsync(orderDto, userId);
if (!success)
{
return BadRequest(new { Message = errorMessage });
}
// Hier wird GetMyOrderById referenziert, also erstellen wir eine leere Methode dafür
return CreatedAtAction(nameof(GetMyOrderById), new { id = createdOrder.Id }, createdOrder);
}
[HttpGet]
public async Task<ActionResult<IEnumerable<OrderSummaryDto>>> GetMyOrders()
[ProducesResponseType(typeof(IEnumerable<OrderSummaryDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<IActionResult> GetMyOrders()
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized();
var orders = await _orderService.GetMyOrdersAsync(userId);
return Ok(orders);
var result = await _orderService.GetMyOrdersAsync(userId);
return Ok(result.Value);
}
[HttpGet("{id}")]
public async Task<ActionResult<OrderDetailDto>> GetMyOrderById(Guid id)
[ProducesResponseType(typeof(OrderDetailDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetMyOrderById(Guid id)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId)) return Unauthorized();
var order = await _orderService.GetMyOrderByIdAsync(id, userId);
if (order == null) return NotFound();
var result = await _orderService.GetMyOrderByIdAsync(id, userId);
return Ok(order);
return result.Type switch
{
ServiceResultType.Success => Ok(result.Value),
ServiceResultType.Forbidden => Forbid(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, "Ein unerwarteter Fehler ist aufgetreten.")
};
}
// HINWEIS: Die Logik zur Erstellung einer Bestellung wurde in den CheckoutController verschoben,
// da dies dem typischen Workflow entspricht. Falls du diesen Endpunkt hier dennoch benötigst,
// kannst du ihn nach dem gleichen Muster wie im CheckoutController implementieren.
}
}