78 lines
3.0 KiB
C#
78 lines
3.0 KiB
C#
// src/Webshop.Api/Controllers/Admin/AdminOrdersController.cs
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using Webshop.Application;
|
||
using Webshop.Application.DTOs.Orders;
|
||
using Webshop.Application.Services.Admin;
|
||
using Webshop.Domain.Enums;
|
||
|
||
namespace Webshop.Api.Controllers.Admin
|
||
{
|
||
[ApiController]
|
||
[Route("api/v1/admin/[controller]")]
|
||
[Authorize(Roles = "Admin")]
|
||
public class AdminOrdersController : ControllerBase
|
||
{
|
||
private readonly IAdminOrderService _adminOrderService;
|
||
|
||
public AdminOrdersController(IAdminOrderService adminOrderService)
|
||
{
|
||
_adminOrderService = adminOrderService;
|
||
}
|
||
|
||
[HttpGet]
|
||
[ProducesResponseType(typeof(IEnumerable<OrderSummaryDto>), StatusCodes.Status200OK)]
|
||
public async Task<IActionResult> GetAllOrders()
|
||
{
|
||
var result = await _adminOrderService.GetAllOrdersAsync();
|
||
return Ok(result.Value);
|
||
}
|
||
|
||
[HttpGet("{id}")]
|
||
[ProducesResponseType(typeof(OrderDetailDto), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> GetOrderById(Guid id)
|
||
{
|
||
var result = await _adminOrderService.GetOrderByIdAsync(id);
|
||
|
||
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")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> UpdateOrderStatus(Guid id, [FromBody] UpdateOrderStatusRequest request)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState);
|
||
}
|
||
|
||
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
|
||
public class UpdateOrderStatusRequest
|
||
{
|
||
public OrderStatus NewStatus { get; set; }
|
||
}
|
||
}
|
||
} |