adminpaymentmethods

This commit is contained in:
Tizian.Breuch
2025-09-25 14:17:42 +02:00
parent 7e6e3c051b
commit 39bd2fb5b3
3 changed files with 109 additions and 76 deletions

View File

@@ -1,17 +1,19 @@
// src/Webshop.Api/Controllers/Admin/AdminPaymentMethodsController.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.DTOs.Payments; // AdminPaymentMethodDto
using Webshop.Application.Services.Admin; // IAdminPaymentMethodService
using Webshop.Application;
using Webshop.Application.DTOs.Payments;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")] // Saubere Route
[Authorize(Roles = "Admin")] // Nur Admins
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminPaymentMethodsController : ControllerBase
{
private readonly IAdminPaymentMethodService _adminPaymentMethodService;
@@ -22,45 +24,87 @@ namespace Webshop.Api.Controllers.Admin
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AdminPaymentMethodDto>>> GetAllPaymentMethods()
[ProducesResponseType(typeof(IEnumerable<AdminPaymentMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllPaymentMethods()
{
var paymentMethods = await _adminPaymentMethodService.GetAllAsync();
return Ok(paymentMethods);
var result = await _adminPaymentMethodService.GetAllAsync();
return Ok(result.Value);
}
[HttpGet("{id}")]
public async Task<ActionResult<AdminPaymentMethodDto>> GetPaymentMethodById(Guid id)
[ProducesResponseType(typeof(AdminPaymentMethodDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetPaymentMethodById(Guid id)
{
var paymentMethod = await _adminPaymentMethodService.GetByIdAsync(id);
if (paymentMethod == null) return NotFound();
return Ok(paymentMethod);
var result = await _adminPaymentMethodService.GetByIdAsync(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." })
};
}
[HttpPost]
public async Task<ActionResult<AdminPaymentMethodDto>> CreatePaymentMethod([FromBody] AdminPaymentMethodDto paymentMethodDto)
[ProducesResponseType(typeof(AdminPaymentMethodDto), StatusCodes.Status201Created)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> CreatePaymentMethod([FromBody] AdminPaymentMethodDto paymentMethodDto)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var createdPaymentMethod = await _adminPaymentMethodService.CreateAsync(paymentMethodDto);
return CreatedAtAction(nameof(GetPaymentMethodById), new { id = createdPaymentMethod.Id }, createdPaymentMethod);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _adminPaymentMethodService.CreateAsync(paymentMethodDto);
return result.Type switch
{
ServiceResultType.Success => CreatedAtAction(nameof(GetPaymentMethodById), new { id = result.Value!.Id }, result.Value),
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
[HttpPut("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdatePaymentMethod(Guid id, [FromBody] AdminPaymentMethodDto paymentMethodDto)
{
if (id != paymentMethodDto.Id) return BadRequest();
if (!ModelState.IsValid) return BadRequest(ModelState);
if (id != paymentMethodDto.Id)
{
return BadRequest(new { Message = "ID in der URL und im Body stimmen nicht überein." });
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var success = await _adminPaymentMethodService.UpdateAsync(paymentMethodDto);
if (!success) return NotFound();
return NoContent(); // 204 No Content für erfolgreiches Update
var result = await _adminPaymentMethodService.UpdateAsync(paymentMethodDto);
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." })
};
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeletePaymentMethod(Guid id)
{
var success = await _adminPaymentMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent();
var result = await _adminPaymentMethodService.DeleteAsync(id);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
}
}