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 // src/Webshop.Api/Controllers/Admin/AdminPaymentMethodsController.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.DTOs.Payments; // AdminPaymentMethodDto using Webshop.Application;
using Webshop.Application.Services.Admin; // IAdminPaymentMethodService using Webshop.Application.DTOs.Payments;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin namespace Webshop.Api.Controllers.Admin
{ {
[ApiController] [ApiController]
[Route("api/v1/admin/[controller]")] // Saubere Route [Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")] // Nur Admins [Authorize(Roles = "Admin")]
public class AdminPaymentMethodsController : ControllerBase public class AdminPaymentMethodsController : ControllerBase
{ {
private readonly IAdminPaymentMethodService _adminPaymentMethodService; private readonly IAdminPaymentMethodService _adminPaymentMethodService;
@@ -22,45 +24,87 @@ namespace Webshop.Api.Controllers.Admin
} }
[HttpGet] [HttpGet]
public async Task<ActionResult<IEnumerable<AdminPaymentMethodDto>>> GetAllPaymentMethods() [ProducesResponseType(typeof(IEnumerable<AdminPaymentMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllPaymentMethods()
{ {
var paymentMethods = await _adminPaymentMethodService.GetAllAsync(); var result = await _adminPaymentMethodService.GetAllAsync();
return Ok(paymentMethods); return Ok(result.Value);
} }
[HttpGet("{id}")] [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); var result = await _adminPaymentMethodService.GetByIdAsync(id);
if (paymentMethod == null) return NotFound();
return Ok(paymentMethod); 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] [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); if (!ModelState.IsValid)
var createdPaymentMethod = await _adminPaymentMethodService.CreateAsync(paymentMethodDto); {
return CreatedAtAction(nameof(GetPaymentMethodById), new { id = createdPaymentMethod.Id }, createdPaymentMethod); 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}")] [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) public async Task<IActionResult> UpdatePaymentMethod(Guid id, [FromBody] AdminPaymentMethodDto paymentMethodDto)
{ {
if (id != paymentMethodDto.Id) return BadRequest(); if (id != paymentMethodDto.Id)
if (!ModelState.IsValid) return BadRequest(ModelState); {
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); var result = await _adminPaymentMethodService.UpdateAsync(paymentMethodDto);
if (!success) return NotFound();
return NoContent(); // 204 No Content für erfolgreiches Update 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}")] [HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> DeletePaymentMethod(Guid id) public async Task<IActionResult> DeletePaymentMethod(Guid id)
{ {
var success = await _adminPaymentMethodService.DeleteAsync(id); var result = await _adminPaymentMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent(); 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." })
};
} }
} }
} }

View File

@@ -2,12 +2,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text.Json; // Wichtig für JsonSerializer using System.Text.Json;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // AdminPaymentMethodDto und die neuen Configuration-DTOs using Webshop.Application;
using Webshop.Domain.Entities; // PaymentMethod using Webshop.Application.DTOs.Payments;
using Webshop.Domain.Enums; // PaymentGatewayType using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces; // IPaymentMethodRepository using Webshop.Domain.Enums;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin namespace Webshop.Application.Services.Admin
{ {
@@ -20,49 +21,51 @@ namespace Webshop.Application.Services.Admin
_paymentMethodRepository = paymentMethodRepository; _paymentMethodRepository = paymentMethodRepository;
} }
public async Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync() public async Task<ServiceResult<IEnumerable<AdminPaymentMethodDto>>> GetAllAsync()
{ {
var paymentMethods = await _paymentMethodRepository.GetAllAsync(); var paymentMethods = await _paymentMethodRepository.GetAllAsync();
var dtos = paymentMethods.Select(pm => new AdminPaymentMethodDto
return paymentMethods.Select(pm => new AdminPaymentMethodDto
{ {
Id = pm.Id, Id = pm.Id,
Name = pm.Name, Name = pm.Name,
Description = pm.Description, Description = pm.Description,
IsActive = pm.IsActive, IsActive = pm.IsActive,
PaymentGatewayType = pm.PaymentGatewayType, PaymentGatewayType = pm.PaymentGatewayType,
// Deserialisiere den JSON-String aus der DB in ein Objekt für die Admin-Ansicht
Configuration = pm.Configuration != null ? JsonSerializer.Deserialize<object>(pm.Configuration) : null, Configuration = pm.Configuration != null ? JsonSerializer.Deserialize<object>(pm.Configuration) : null,
ProcessingFee = pm.ProcessingFee ProcessingFee = pm.ProcessingFee
}).ToList(); }).ToList();
return ServiceResult.Ok<IEnumerable<AdminPaymentMethodDto>>(dtos);
} }
public async Task<AdminPaymentMethodDto?> GetByIdAsync(Guid id) public async Task<ServiceResult<AdminPaymentMethodDto>> GetByIdAsync(Guid id)
{ {
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id); var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return null; if (paymentMethod == null)
{
return ServiceResult.Fail<AdminPaymentMethodDto>(ServiceResultType.NotFound, $"Zahlungsmethode mit ID '{id}' nicht gefunden.");
}
return new AdminPaymentMethodDto var dto = new AdminPaymentMethodDto
{ {
Id = paymentMethod.Id, Id = paymentMethod.Id,
Name = paymentMethod.Name, Name = paymentMethod.Name,
Description = paymentMethod.Description, Description = paymentMethod.Description,
IsActive = paymentMethod.IsActive, IsActive = paymentMethod.IsActive,
PaymentGatewayType = paymentMethod.PaymentGatewayType, PaymentGatewayType = paymentMethod.PaymentGatewayType,
// Deserialisiere den JSON-String aus der DB in ein Objekt
Configuration = paymentMethod.Configuration != null ? JsonSerializer.Deserialize<object>(paymentMethod.Configuration) : null, Configuration = paymentMethod.Configuration != null ? JsonSerializer.Deserialize<object>(paymentMethod.Configuration) : null,
ProcessingFee = paymentMethod.ProcessingFee ProcessingFee = paymentMethod.ProcessingFee
}; };
return ServiceResult.Ok(dto);
} }
public async Task<AdminPaymentMethodDto> CreateAsync(AdminPaymentMethodDto paymentMethodDto) public async Task<ServiceResult<AdminPaymentMethodDto>> CreateAsync(AdminPaymentMethodDto paymentMethodDto)
{ {
var (isValid, configJson, errorMessage) = ValidateAndSerializeConfiguration(paymentMethodDto.PaymentGatewayType, paymentMethodDto.Configuration); var (isValid, configJson, errorMessage) = ValidateAndSerializeConfiguration(paymentMethodDto.PaymentGatewayType, paymentMethodDto.Configuration);
if (!isValid) if (!isValid)
{ {
// In einer echten Anwendung würden Sie hier eine benutzerdefinierte Validierungs-Exception werfen, return ServiceResult.Fail<AdminPaymentMethodDto>(ServiceResultType.InvalidInput, errorMessage!);
// damit der Controller einen 400 Bad Request zurückgeben kann.
throw new ArgumentException(errorMessage);
} }
var newPaymentMethod = new PaymentMethod var newPaymentMethod = new PaymentMethod
@@ -72,25 +75,28 @@ namespace Webshop.Application.Services.Admin
Description = paymentMethodDto.Description, Description = paymentMethodDto.Description,
IsActive = paymentMethodDto.IsActive, IsActive = paymentMethodDto.IsActive,
PaymentGatewayType = paymentMethodDto.PaymentGatewayType, PaymentGatewayType = paymentMethodDto.PaymentGatewayType,
Configuration = configJson, // Speichere den validierten JSON-String Configuration = configJson,
ProcessingFee = paymentMethodDto.ProcessingFee ProcessingFee = paymentMethodDto.ProcessingFee
}; };
await _paymentMethodRepository.AddAsync(newPaymentMethod); await _paymentMethodRepository.AddAsync(newPaymentMethod);
paymentMethodDto.Id = newPaymentMethod.Id; paymentMethodDto.Id = newPaymentMethod.Id;
return paymentMethodDto; return ServiceResult.Ok(paymentMethodDto);
} }
public async Task<bool> UpdateAsync(AdminPaymentMethodDto paymentMethodDto) public async Task<ServiceResult> UpdateAsync(AdminPaymentMethodDto paymentMethodDto)
{ {
var existingPaymentMethod = await _paymentMethodRepository.GetByIdAsync(paymentMethodDto.Id); var existingPaymentMethod = await _paymentMethodRepository.GetByIdAsync(paymentMethodDto.Id);
if (existingPaymentMethod == null) return false; if (existingPaymentMethod == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Zahlungsmethode mit ID '{paymentMethodDto.Id}' nicht gefunden.");
}
var (isValid, configJson, errorMessage) = ValidateAndSerializeConfiguration(paymentMethodDto.PaymentGatewayType, paymentMethodDto.Configuration); var (isValid, configJson, errorMessage) = ValidateAndSerializeConfiguration(paymentMethodDto.PaymentGatewayType, paymentMethodDto.Configuration);
if (!isValid) if (!isValid)
{ {
throw new ArgumentException(errorMessage); return ServiceResult.Fail(ServiceResultType.InvalidInput, errorMessage!);
} }
existingPaymentMethod.Name = paymentMethodDto.Name; existingPaymentMethod.Name = paymentMethodDto.Name;
@@ -101,16 +107,19 @@ namespace Webshop.Application.Services.Admin
existingPaymentMethod.ProcessingFee = paymentMethodDto.ProcessingFee; existingPaymentMethod.ProcessingFee = paymentMethodDto.ProcessingFee;
await _paymentMethodRepository.UpdateAsync(existingPaymentMethod); await _paymentMethodRepository.UpdateAsync(existingPaymentMethod);
return true; return ServiceResult.Ok();
} }
public async Task<bool> DeleteAsync(Guid id) public async Task<ServiceResult> DeleteAsync(Guid id)
{ {
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id); var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return false; if (paymentMethod == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Zahlungsmethode mit ID '{id}' nicht gefunden.");
}
await _paymentMethodRepository.DeleteAsync(id); await _paymentMethodRepository.DeleteAsync(id);
return true; return ServiceResult.Ok();
} }
private (bool IsValid, string? ConfigJson, string? ErrorMessage) ValidateAndSerializeConfiguration(PaymentGatewayType type, object? configObject) private (bool IsValid, string? ConfigJson, string? ErrorMessage) ValidateAndSerializeConfiguration(PaymentGatewayType type, object? configObject)
@@ -148,8 +157,6 @@ namespace Webshop.Application.Services.Admin
return (true, JsonSerializer.Serialize(payPalConfig), null); return (true, JsonSerializer.Serialize(payPalConfig), null);
default: default:
// Für Typen wie Invoice oder CashOnDelivery, die keine spezielle Konfiguration brauchen,
// ist ein leeres JSON-Objekt '{}' ein gültiger Wert.
return (true, JsonSerializer.Serialize(new object()), null); return (true, JsonSerializer.Serialize(new object()), null);
} }
} }

View File

@@ -2,35 +2,17 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // Für AdminPaymentMethodDto using Webshop.Application;
using Webshop.Application.DTOs.Payments;
namespace Webshop.Application.Services.Admin namespace Webshop.Application.Services.Admin
{ {
public interface IAdminPaymentMethodService public interface IAdminPaymentMethodService
{ {
/// <summary> Task<ServiceResult<IEnumerable<AdminPaymentMethodDto>>> GetAllAsync();
/// Ruft alle Zahlungsmethoden ab (sowohl aktive als auch inaktive). Task<ServiceResult<AdminPaymentMethodDto>> GetByIdAsync(Guid id);
/// </summary> Task<ServiceResult<AdminPaymentMethodDto>> CreateAsync(AdminPaymentMethodDto paymentMethodDto);
Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync(); Task<ServiceResult> UpdateAsync(AdminPaymentMethodDto paymentMethodDto);
Task<ServiceResult> DeleteAsync(Guid id);
/// <summary>
/// Ruft eine einzelne Zahlungsmethode anhand ihrer ID ab.
/// </summary>
Task<AdminPaymentMethodDto?> GetByIdAsync(Guid id);
/// <summary>
/// Erstellt eine neue Zahlungsmethode.
/// </summary>
Task<AdminPaymentMethodDto> CreateAsync(AdminPaymentMethodDto paymentMethodDto);
/// <summary>
/// Aktualisiert eine bestehende Zahlungsmethode.
/// </summary>
Task<bool> UpdateAsync(AdminPaymentMethodDto paymentMethodDto);
/// <summary>
/// Löscht eine Zahlungsmethode anhand ihrer ID.
/// </summary>
Task<bool> DeleteAsync(Guid id);
} }
} }