adminorder
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
// src/Webshop.Api/Controllers/Admin/AdminOrdersController.cs
|
// src/Webshop.Api/Controllers/Admin/AdminOrdersController.cs
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Orders;
|
using Webshop.Application.DTOs.Orders;
|
||||||
using Webshop.Application.Services.Admin;
|
using Webshop.Application.Services.Admin;
|
||||||
using Webshop.Domain.Enums;
|
using Webshop.Domain.Enums;
|
||||||
@@ -23,29 +25,48 @@ namespace Webshop.Api.Controllers.Admin
|
|||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<IEnumerable<OrderSummaryDto>>> GetAllOrders()
|
[ProducesResponseType(typeof(IEnumerable<OrderSummaryDto>), StatusCodes.Status200OK)]
|
||||||
|
public async Task<IActionResult> GetAllOrders()
|
||||||
{
|
{
|
||||||
var orders = await _adminOrderService.GetAllOrdersAsync();
|
var result = await _adminOrderService.GetAllOrdersAsync();
|
||||||
return Ok(orders);
|
return Ok(result.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpGet("{id}")]
|
[HttpGet("{id}")]
|
||||||
public async Task<ActionResult<OrderDetailDto>> GetOrderById(Guid id)
|
[ProducesResponseType(typeof(OrderDetailDto), StatusCodes.Status200OK)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
|
public async Task<IActionResult> GetOrderById(Guid id)
|
||||||
{
|
{
|
||||||
var order = await _adminOrderService.GetOrderByIdAsync(id);
|
var result = await _adminOrderService.GetOrderByIdAsync(id);
|
||||||
if (order == null) return NotFound();
|
|
||||||
return Ok(order);
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => Ok(result.Value),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPut("{id}/status")]
|
[HttpPut("{id}/status")]
|
||||||
|
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||||||
|
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||||
public async Task<IActionResult> UpdateOrderStatus(Guid id, [FromBody] UpdateOrderStatusRequest request)
|
public async Task<IActionResult> UpdateOrderStatus(Guid id, [FromBody] UpdateOrderStatusRequest request)
|
||||||
{
|
{
|
||||||
var (success, errorMessage) = await _adminOrderService.UpdateOrderStatusAsync(id, request.NewStatus);
|
if (!ModelState.IsValid)
|
||||||
if (!success)
|
|
||||||
{
|
{
|
||||||
return BadRequest(new { Message = errorMessage });
|
return BadRequest(ModelState);
|
||||||
}
|
}
|
||||||
return NoContent();
|
|
||||||
|
var result = await _adminOrderService.UpdateOrderStatusAsync(id, request.NewStatus);
|
||||||
|
|
||||||
|
return result.Type switch
|
||||||
|
{
|
||||||
|
ServiceResultType.Success => NoContent(),
|
||||||
|
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||||||
|
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
|
||||||
|
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ein kleines DTO f<>r die Status-Update-Anfrage
|
// Ein kleines DTO f<>r die Status-Update-Anfrage
|
||||||
|
|||||||
@@ -3,9 +3,10 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Customers;
|
using Webshop.Application.DTOs.Customers;
|
||||||
using Webshop.Application.DTOs.Orders;
|
using Webshop.Application.DTOs.Orders;
|
||||||
using Webshop.Domain.Enums; // Wichtig für Enum-Konvertierung
|
using Webshop.Domain.Enums;
|
||||||
using Webshop.Domain.Interfaces;
|
using Webshop.Domain.Interfaces;
|
||||||
|
|
||||||
namespace Webshop.Application.Services.Admin
|
namespace Webshop.Application.Services.Admin
|
||||||
@@ -19,28 +20,32 @@ namespace Webshop.Application.Services.Admin
|
|||||||
_orderRepository = orderRepository;
|
_orderRepository = orderRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<OrderSummaryDto>> GetAllOrdersAsync()
|
public async Task<ServiceResult<IEnumerable<OrderSummaryDto>>> GetAllOrdersAsync()
|
||||||
{
|
{
|
||||||
var orders = await _orderRepository.GetAllAsync();
|
var orders = await _orderRepository.GetAllAsync();
|
||||||
return orders.Select(o => new OrderSummaryDto
|
var dtos = orders.Select(o => new OrderSummaryDto
|
||||||
{
|
{
|
||||||
Id = o.Id,
|
Id = o.Id,
|
||||||
OrderNumber = o.OrderNumber,
|
OrderNumber = o.OrderNumber,
|
||||||
OrderDate = o.OrderDate,
|
OrderDate = o.OrderDate,
|
||||||
CustomerName = o.Customer != null ? $"{o.Customer.FirstName} {o.Customer.LastName}" : o.GuestEmail ?? "Gastbestellung",
|
CustomerName = o.Customer != null ? $"{o.Customer.FirstName} {o.Customer.LastName}" : o.GuestEmail ?? "Gastbestellung",
|
||||||
TotalAmount = o.OrderTotal,
|
TotalAmount = o.OrderTotal,
|
||||||
// << KORREKTUR: String zu Enum parsen >>
|
|
||||||
Status = Enum.TryParse<OrderStatus>(o.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
|
Status = Enum.TryParse<OrderStatus>(o.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
|
||||||
PaymentStatus = Enum.TryParse<PaymentStatus>(o.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending
|
PaymentStatus = Enum.TryParse<PaymentStatus>(o.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending
|
||||||
}).ToList();
|
}).ToList();
|
||||||
|
|
||||||
|
return ServiceResult.Ok<IEnumerable<OrderSummaryDto>>(dtos);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<OrderDetailDto?> GetOrderByIdAsync(Guid id)
|
public async Task<ServiceResult<OrderDetailDto>> GetOrderByIdAsync(Guid id)
|
||||||
{
|
{
|
||||||
var order = await _orderRepository.GetByIdAsync(id);
|
var order = await _orderRepository.GetByIdAsync(id);
|
||||||
if (order == null) return null;
|
if (order == null)
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail<OrderDetailDto>(ServiceResultType.NotFound, $"Bestellung mit ID '{id}' nicht gefunden.");
|
||||||
|
}
|
||||||
|
|
||||||
return new OrderDetailDto
|
var orderDetailDto = new OrderDetailDto
|
||||||
{
|
{
|
||||||
Id = order.Id,
|
Id = order.Id,
|
||||||
OrderNumber = order.OrderNumber,
|
OrderNumber = order.OrderNumber,
|
||||||
@@ -48,8 +53,6 @@ namespace Webshop.Application.Services.Admin
|
|||||||
CustomerId = order.CustomerId,
|
CustomerId = order.CustomerId,
|
||||||
Status = Enum.TryParse<OrderStatus>(order.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
|
Status = Enum.TryParse<OrderStatus>(order.OrderStatus, true, out var orderStatus) ? orderStatus : OrderStatus.Pending,
|
||||||
TotalAmount = order.OrderTotal,
|
TotalAmount = order.OrderTotal,
|
||||||
|
|
||||||
// << KORREKTUR: Manuelles Mapping von Address-Entität zu AddressDto >>
|
|
||||||
ShippingAddress = new AddressDto
|
ShippingAddress = new AddressDto
|
||||||
{
|
{
|
||||||
Id = order.ShippingAddress.Id,
|
Id = order.ShippingAddress.Id,
|
||||||
@@ -70,7 +73,6 @@ namespace Webshop.Application.Services.Admin
|
|||||||
Country = order.BillingAddress.Country,
|
Country = order.BillingAddress.Country,
|
||||||
Type = order.BillingAddress.Type
|
Type = order.BillingAddress.Type
|
||||||
},
|
},
|
||||||
|
|
||||||
PaymentMethod = order.PaymentMethod,
|
PaymentMethod = order.PaymentMethod,
|
||||||
PaymentStatus = Enum.TryParse<PaymentStatus>(order.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending,
|
PaymentStatus = Enum.TryParse<PaymentStatus>(order.PaymentStatus, true, out var paymentStatus) ? paymentStatus : PaymentStatus.Pending,
|
||||||
OrderItems = order.OrderItems.Select(oi => new OrderItemDto
|
OrderItems = order.OrderItems.Select(oi => new OrderItemDto
|
||||||
@@ -85,26 +87,33 @@ namespace Webshop.Application.Services.Admin
|
|||||||
TotalPrice = oi.TotalPrice
|
TotalPrice = oi.TotalPrice
|
||||||
}).ToList()
|
}).ToList()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return ServiceResult.Ok(orderDetailDto);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<(bool Success, string? ErrorMessage)> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus)
|
public async Task<ServiceResult> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus)
|
||||||
{
|
{
|
||||||
|
if (!Enum.IsDefined(typeof(OrderStatus), newStatus))
|
||||||
|
{
|
||||||
|
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Der Status '{newStatus}' ist ungültig.");
|
||||||
|
}
|
||||||
|
|
||||||
var order = await _orderRepository.GetByIdAsync(id);
|
var order = await _orderRepository.GetByIdAsync(id);
|
||||||
if (order == null)
|
if (order == null)
|
||||||
{
|
{
|
||||||
return (false, "Bestellung nicht gefunden.");
|
return ServiceResult.Fail(ServiceResultType.NotFound, $"Bestellung mit ID '{id}' nicht gefunden.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// << KORREKTUR: Enum zu String konvertieren >>
|
|
||||||
order.OrderStatus = newStatus.ToString();
|
order.OrderStatus = newStatus.ToString();
|
||||||
|
|
||||||
|
// Hier könnte zusätzliche Logik stehen (z.B. Tracking-Nummer verlangen)
|
||||||
if (newStatus == OrderStatus.Shipped && string.IsNullOrEmpty(order.ShippingTrackingNumber))
|
if (newStatus == OrderStatus.Shipped && string.IsNullOrEmpty(order.ShippingTrackingNumber))
|
||||||
{
|
{
|
||||||
// Hier könnten Sie Logik hinzufügen, um eine Tracking-Nummer zu verlangen
|
// return ServiceResult.Fail(ServiceResultType.InvalidInput, "Für den Status 'Versandt' wird eine Tracking-Nummer benötigt.");
|
||||||
}
|
}
|
||||||
|
|
||||||
await _orderRepository.UpdateAsync(order);
|
await _orderRepository.UpdateAsync(order);
|
||||||
return (true, null);
|
return ServiceResult.Ok();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Webshop.Application;
|
||||||
using Webshop.Application.DTOs.Orders;
|
using Webshop.Application.DTOs.Orders;
|
||||||
using Webshop.Domain.Enums;
|
using Webshop.Domain.Enums;
|
||||||
|
|
||||||
@@ -9,8 +10,8 @@ namespace Webshop.Application.Services.Admin
|
|||||||
{
|
{
|
||||||
public interface IAdminOrderService
|
public interface IAdminOrderService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<OrderSummaryDto>> GetAllOrdersAsync();
|
Task<ServiceResult<IEnumerable<OrderSummaryDto>>> GetAllOrdersAsync();
|
||||||
Task<OrderDetailDto?> GetOrderByIdAsync(Guid id);
|
Task<ServiceResult<OrderDetailDto>> GetOrderByIdAsync(Guid id);
|
||||||
Task<(bool Success, string? ErrorMessage)> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus);
|
Task<ServiceResult> UpdateOrderStatusAsync(Guid id, OrderStatus newStatus);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user