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

@@ -2,12 +2,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json; // Wichtig für JsonSerializer
using System.Text.Json;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // AdminPaymentMethodDto und die neuen Configuration-DTOs
using Webshop.Domain.Entities; // PaymentMethod
using Webshop.Domain.Enums; // PaymentGatewayType
using Webshop.Domain.Interfaces; // IPaymentMethodRepository
using Webshop.Application;
using Webshop.Application.DTOs.Payments;
using Webshop.Domain.Entities;
using Webshop.Domain.Enums;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin
{
@@ -20,49 +21,51 @@ namespace Webshop.Application.Services.Admin
_paymentMethodRepository = paymentMethodRepository;
}
public async Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync()
public async Task<ServiceResult<IEnumerable<AdminPaymentMethodDto>>> GetAllAsync()
{
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
return paymentMethods.Select(pm => new AdminPaymentMethodDto
var dtos = paymentMethods.Select(pm => new AdminPaymentMethodDto
{
Id = pm.Id,
Name = pm.Name,
Description = pm.Description,
IsActive = pm.IsActive,
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,
ProcessingFee = pm.ProcessingFee
}).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);
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,
Name = paymentMethod.Name,
Description = paymentMethod.Description,
IsActive = paymentMethod.IsActive,
PaymentGatewayType = paymentMethod.PaymentGatewayType,
// Deserialisiere den JSON-String aus der DB in ein Objekt
Configuration = paymentMethod.Configuration != null ? JsonSerializer.Deserialize<object>(paymentMethod.Configuration) : null,
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);
if (!isValid)
{
// In einer echten Anwendung würden Sie hier eine benutzerdefinierte Validierungs-Exception werfen,
// damit der Controller einen 400 Bad Request zurückgeben kann.
throw new ArgumentException(errorMessage);
return ServiceResult.Fail<AdminPaymentMethodDto>(ServiceResultType.InvalidInput, errorMessage!);
}
var newPaymentMethod = new PaymentMethod
@@ -72,25 +75,28 @@ namespace Webshop.Application.Services.Admin
Description = paymentMethodDto.Description,
IsActive = paymentMethodDto.IsActive,
PaymentGatewayType = paymentMethodDto.PaymentGatewayType,
Configuration = configJson, // Speichere den validierten JSON-String
Configuration = configJson,
ProcessingFee = paymentMethodDto.ProcessingFee
};
await _paymentMethodRepository.AddAsync(newPaymentMethod);
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);
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);
if (!isValid)
{
throw new ArgumentException(errorMessage);
return ServiceResult.Fail(ServiceResultType.InvalidInput, errorMessage!);
}
existingPaymentMethod.Name = paymentMethodDto.Name;
@@ -101,16 +107,19 @@ namespace Webshop.Application.Services.Admin
existingPaymentMethod.ProcessingFee = paymentMethodDto.ProcessingFee;
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);
if (paymentMethod == null) return false;
if (paymentMethod == null)
{
return ServiceResult.Fail(ServiceResultType.NotFound, $"Zahlungsmethode mit ID '{id}' nicht gefunden.");
}
await _paymentMethodRepository.DeleteAsync(id);
return true;
return ServiceResult.Ok();
}
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);
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);
}
}