adminpaymentmethods
This commit is contained in:
@@ -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." })
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,35 +2,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
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
|
||||
{
|
||||
public interface IAdminPaymentMethodService
|
||||
{
|
||||
/// <summary>
|
||||
/// Ruft alle Zahlungsmethoden ab (sowohl aktive als auch inaktive).
|
||||
/// </summary>
|
||||
Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync();
|
||||
|
||||
/// <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);
|
||||
Task<ServiceResult<IEnumerable<AdminPaymentMethodDto>>> GetAllAsync();
|
||||
Task<ServiceResult<AdminPaymentMethodDto>> GetByIdAsync(Guid id);
|
||||
Task<ServiceResult<AdminPaymentMethodDto>> CreateAsync(AdminPaymentMethodDto paymentMethodDto);
|
||||
Task<ServiceResult> UpdateAsync(AdminPaymentMethodDto paymentMethodDto);
|
||||
Task<ServiceResult> DeleteAsync(Guid id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user