This commit is contained in:
Tizian.Breuch
2025-07-29 19:11:34 +02:00
parent 3907cba29d
commit 6fc6aaef3e
14 changed files with 375 additions and 31 deletions

View File

@@ -0,0 +1,66 @@
// src/Webshop.Api/Controllers/Admin/AdminPaymentMethodsController.cs
using Microsoft.AspNetCore.Authorization;
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
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/paymentmethods")] // Saubere Route
[Authorize(Roles = "Admin")] // Nur Admins
public class AdminPaymentMethodsController : ControllerBase
{
private readonly IAdminPaymentMethodService _adminPaymentMethodService;
public AdminPaymentMethodsController(IAdminPaymentMethodService adminPaymentMethodService)
{
_adminPaymentMethodService = adminPaymentMethodService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<AdminPaymentMethodDto>>> GetAllPaymentMethods()
{
var paymentMethods = await _adminPaymentMethodService.GetAllAsync();
return Ok(paymentMethods);
}
[HttpGet("{id}")]
public async Task<ActionResult<AdminPaymentMethodDto>> GetPaymentMethodById(Guid id)
{
var paymentMethod = await _adminPaymentMethodService.GetByIdAsync(id);
if (paymentMethod == null) return NotFound();
return Ok(paymentMethod);
}
[HttpPost]
public async Task<ActionResult<AdminPaymentMethodDto>> 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);
}
[HttpPut("{id}")]
public async Task<IActionResult> UpdatePaymentMethod(Guid id, [FromBody] AdminPaymentMethodDto paymentMethodDto)
{
if (id != paymentMethodDto.Id) return BadRequest();
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
}
[HttpDelete("{id}")]
public async Task<IActionResult> DeletePaymentMethod(Guid id)
{
var success = await _adminPaymentMethodService.DeleteAsync(id);
if (!success) return NotFound();
return NoContent();
}
}
}

View File

@@ -0,0 +1,30 @@
// src/Webshop.Api/Controllers/Public/PaymentMethodsController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // PaymentMethodDto
using Webshop.Application.Services.Public; // IPaymentMethodService
namespace Webshop.Api.Controllers.Public
{
[ApiController]
[Route("api/v1/public/paymentmethods")] // Saubere Route
[AllowAnonymous] // Jeder darf die verfügbaren Zahlungsmethoden sehen
public class PaymentMethodsController : ControllerBase
{
private readonly IPaymentMethodService _paymentMethodService;
public PaymentMethodsController(IPaymentMethodService paymentMethodService)
{
_paymentMethodService = paymentMethodService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<PaymentMethodDto>>> GetActivePaymentMethods()
{
var paymentMethods = await _paymentMethodService.GetAllActiveAsync();
return Ok(paymentMethods);
}
}
}

View File

@@ -85,11 +85,13 @@ builder.Services.AddScoped<IAuthService, AuthService>();
// PUBLIC Services // PUBLIC Services
builder.Services.AddScoped<IProductService, ProductService>(); builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddScoped<IPaymentMethodService, PaymentMethodService>();
// ADMIN Services // ADMIN Services
builder.Services.AddScoped<IAdminUserService, AdminUserService>(); builder.Services.AddScoped<IAdminUserService, AdminUserService>();
builder.Services.AddScoped<IAdminProductService, AdminProductService>(); builder.Services.AddScoped<IAdminProductService, AdminProductService>();
builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>(); builder.Services.AddScoped<IAdminSupplierService, AdminSupplierService>();
builder.Services.AddScoped<IAdminPaymentMethodService, AdminPaymentMethodService>();
// CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen) // CUSTOMER Services (sp<73>ter Implementierungen hinzuf<75>gen)
builder.Services.AddScoped<ICustomerService, CustomerService>(); builder.Services.AddScoped<ICustomerService, CustomerService>();

View File

@@ -16,6 +16,7 @@ using Webshop.Application.DTOs.Orders;
using Webshop.Application.DTOs.Discounts; using Webshop.Application.DTOs.Discounts;
using Webshop.Application.DTOs.Categorys; using Webshop.Application.DTOs.Categorys;
using Webshop.Api.Controllers.Auth; using Webshop.Api.Controllers.Auth;
using Webshop.Domain.Enums;
namespace Webshop.Api.SwaggerFilters namespace Webshop.Api.SwaggerFilters
{ {

View File

@@ -0,0 +1,17 @@
// src/Webshop.Application/DTOs/Payments/AdminPaymentMethodDto.cs
using System;
using Webshop.Domain.Enums;
namespace Webshop.Application.DTOs.Payments
{
public class AdminPaymentMethodDto
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public bool IsActive { get; set; }
public PaymentGatewayType PaymentGatewayType { get; set; }
public string? Configuration { get; set; } // Als JSON-String, den der Admin bearbeitet
public decimal? ProcessingFee { get; set; }
}
}

View File

@@ -1,7 +1,8 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Application/DTOs/Payments/PaymentMethodDto.cs
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Domain.Enums;
@@ -12,8 +13,10 @@ namespace Webshop.Application.DTOs.Payments
public Guid Id { get; set; } public Guid Id { get; set; }
public string Name { get; set; } = string.Empty; public string Name { get; set; } = string.Empty;
public string? Description { get; set; } public string? Description { get; set; }
public bool IsActive { get; set; } public PaymentGatewayType PaymentGatewayType { get; set; }
public PaymentGatewayType GatewayType { get; set; }
public decimal? ProcessingFee { get; set; } public decimal? ProcessingFee { get; set; }
// Wichtig: Wir geben hier nur <20>ffentliche Konfigurationsdaten preis (z.B. IBAN)
public object? PublicConfiguration { get; set; }
} }
} }

View File

@@ -0,0 +1,100 @@
// src/Webshop.Application/Services/Admin/AdminPaymentMethodService.cs
using System;
using System.Collections.Generic;
using System.Linq; // Für Select
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // AdminPaymentMethodDto
using Webshop.Domain.Entities; // PaymentMethod
using Webshop.Domain.Interfaces; // IPaymentMethodRepository
using Webshop.Domain.Enums;
namespace Webshop.Application.Services.Admin
{
public class AdminPaymentMethodService : IAdminPaymentMethodService
{
private readonly IPaymentMethodRepository _paymentMethodRepository;
public AdminPaymentMethodService(IPaymentMethodRepository paymentMethodRepository)
{
_paymentMethodRepository = paymentMethodRepository;
}
public async Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync()
{
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
return paymentMethods.Select(pm => new AdminPaymentMethodDto
{
Id = pm.Id,
Name = pm.Name,
Description = pm.Description,
IsActive = pm.IsActive,
PaymentGatewayType = pm.PaymentGatewayType,
Configuration = pm.Configuration, // Gibt den JSON-String für den Admin zurück
ProcessingFee = pm.ProcessingFee
}).ToList();
}
public async Task<AdminPaymentMethodDto?> GetByIdAsync(Guid id)
{
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return null;
return new AdminPaymentMethodDto
{
Id = paymentMethod.Id,
Name = paymentMethod.Name,
Description = paymentMethod.Description,
IsActive = paymentMethod.IsActive,
PaymentGatewayType = paymentMethod.PaymentGatewayType,
Configuration = paymentMethod.Configuration,
ProcessingFee = paymentMethod.ProcessingFee
};
}
public async Task<AdminPaymentMethodDto> CreateAsync(AdminPaymentMethodDto paymentMethodDto)
{
var newPaymentMethod = new PaymentMethod
{
Id = Guid.NewGuid(), // API generiert die ID
Name = paymentMethodDto.Name,
Description = paymentMethodDto.Description,
IsActive = paymentMethodDto.IsActive,
PaymentGatewayType = paymentMethodDto.PaymentGatewayType,
Configuration = paymentMethodDto.Configuration,
ProcessingFee = paymentMethodDto.ProcessingFee
};
await _paymentMethodRepository.AddAsync(newPaymentMethod);
paymentMethodDto.Id = newPaymentMethod.Id; // ID zurückschreiben
return paymentMethodDto;
}
public async Task<bool> UpdateAsync(AdminPaymentMethodDto paymentMethodDto)
{
var existingPaymentMethod = await _paymentMethodRepository.GetByIdAsync(paymentMethodDto.Id);
if (existingPaymentMethod == null) return false;
// Eigenschaften aktualisieren
existingPaymentMethod.Name = paymentMethodDto.Name;
existingPaymentMethod.Description = paymentMethodDto.Description;
existingPaymentMethod.IsActive = paymentMethodDto.IsActive;
existingPaymentMethod.PaymentGatewayType = paymentMethodDto.PaymentGatewayType;
existingPaymentMethod.Configuration = paymentMethodDto.Configuration;
existingPaymentMethod.ProcessingFee = paymentMethodDto.ProcessingFee;
await _paymentMethodRepository.UpdateAsync(existingPaymentMethod);
return true;
}
public async Task<bool> DeleteAsync(Guid id)
{
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return false;
await _paymentMethodRepository.DeleteAsync(id);
return true;
}
}
}

View File

@@ -0,0 +1,36 @@
// src/Webshop.Application/Services/Admin/IAdminPaymentMethodService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // Für AdminPaymentMethodDto
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);
}
}

View File

@@ -0,0 +1,16 @@
// src/Webshop.Application/Services/Public/IPaymentMethodService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // Für PaymentMethodDto
namespace Webshop.Application.Services.Public
{
public interface IPaymentMethodService
{
/// <summary>
/// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen.
/// </summary>
/// <returns>Eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten.</returns>
Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync();
}
}

View File

@@ -0,0 +1,67 @@
// src/Webshop.Application/Services/Public/PaymentMethodService.cs
using System.Collections.Generic;
using System.Linq;
using System.Text.Json; // Für JSON-Verarbeitung
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments;
using Webshop.Domain.Enums;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Public
{
public class PaymentMethodService : IPaymentMethodService
{
private readonly IPaymentMethodRepository _paymentMethodRepository;
public PaymentMethodService(IPaymentMethodRepository paymentMethodRepository)
{
_paymentMethodRepository = paymentMethodRepository;
}
public async Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync()
{
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
return paymentMethods
.Where(pm => pm.IsActive)
.Select(pm => new PaymentMethodDto
{
Id = pm.Id,
Name = pm.Name,
Description = pm.Description,
PaymentGatewayType = pm.PaymentGatewayType,
ProcessingFee = pm.ProcessingFee,
PublicConfiguration = GetPublicConfiguration(pm.PaymentGatewayType, pm.Configuration)
}).ToList();
}
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
{
if (string.IsNullOrEmpty(configJson)) return null;
// Beispiel: Nur für BankTransfer geben wir IBAN etc. preis
if (type == PaymentGatewayType.BankTransfer)
{
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
if (config != null)
{
// Filter, um geheime Schlüssel NICHT an das Frontend zu senden
return new { IBAN = config.GetValueOrDefault("IBAN"), BIC = config.GetValueOrDefault("BIC"), BankName = config.GetValueOrDefault("BankName") };
}
}
// Für Stripe/PayPal geben wir nur den Public Key preis
if (type == PaymentGatewayType.Stripe)
{
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
if (config != null)
{
return new { PublicKey = config.GetValueOrDefault("PublicKey") };
}
}
return null; // Für andere Typen geben wir keine Konfigurationsdetails preis
}
}
}

View File

@@ -1,5 +1,7 @@
using System; using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Webshop.Domain.Enums;
namespace Webshop.Domain.Entities; namespace Webshop.Domain.Entities;
@@ -8,22 +10,21 @@ namespace Webshop.Domain.Entities;
/// </summary> /// </summary>
public class PaymentMethod public class PaymentMethod
{ {
[Key] public Guid Id { get; set; } = Guid.NewGuid();
public Guid Id { get; set; }
[Required] [Required]
[MaxLength(100)] [MaxLength(100)]
public string Name { get; set; } public string Name { get; set; } = string.Empty;
[MaxLength(500)] [MaxLength(500)]
public string? Description { get; set; } public string? Description { get; set; }
[Required] public bool IsActive { get; set; } = true;
public bool IsActive { get; set; }
[Required] public PaymentGatewayType PaymentGatewayType { get; set; }
[MaxLength(50)]
public string PaymentGatewayType { get; set; } // "Stripe", "PayPal", etc. [Column(TypeName = "jsonb")]
public string? Configuration { get; set; }
public decimal? ProcessingFee { get; set; } public decimal? ProcessingFee { get; set; }
} }

View File

@@ -1,7 +1,12 @@
public enum PaymentGatewayType // src/Webshop.Domain/Enums/PaymentGatewayType.cs
namespace Webshop.Domain.Enums
{ {
Stripe, public enum PaymentGatewayType
{
BankTransfer,
PayPal, PayPal,
Klarna, Stripe,
Manual // z.B. Vorkasse CashOnDelivery,
Invoice
}
} }

View File

@@ -1,14 +1,16 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Domain/Interfaces/IPaymentMethodRepository.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
namespace Webshop.Domain.Interfaces namespace Webshop.Domain.Interfaces
{ {
public interface IPaymentMethodRepository public interface IPaymentMethodRepository
{ {
// Fügen Sie hier Methodensignaturen hinzu Task<IEnumerable<PaymentMethod>> GetAllAsync();
Task<PaymentMethod?> GetByIdAsync(Guid id);
Task AddAsync(PaymentMethod paymentMethod);
Task UpdateAsync(PaymentMethod paymentMethod);
Task DeleteAsync(Guid id);
} }
} }

View File

@@ -1,4 +1,4 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Infrastructure/Repositories/PaymentMethodRepository.cs
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces; using Webshop.Domain.Interfaces;
@@ -18,12 +18,10 @@ namespace Webshop.Infrastructure.Repositories
_context = context; _context = context;
} }
// Fügen Sie hier Repository-Methoden hinzu public async Task<IEnumerable<PaymentMethod>> GetAllAsync() => await _context.PaymentMethods.ToListAsync();
// Beispiel: public async Task<PaymentMethod?> GetByIdAsync(Guid id) => await _context.PaymentMethods.FindAsync(id);
// public async Task<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().ToListAsync(); } public async Task AddAsync(PaymentMethod paymentMethod) { _context.PaymentMethods.Add(paymentMethod); await _context.SaveChangesAsync(); }
// public async Task<T?> GetByIdAsync(Guid id) { return await _context.Set<T>().FindAsync(id); } public async Task UpdateAsync(PaymentMethod paymentMethod) { _context.PaymentMethods.Update(paymentMethod); await _context.SaveChangesAsync(); }
// public async Task AddAsync(T entity) { _context.Set<T>().Add(entity); await _context.SaveChangesAsync(); } public async Task DeleteAsync(Guid id) { var entity = await _context.PaymentMethods.FindAsync(id); if (entity != null) { _context.PaymentMethods.Remove(entity); await _context.SaveChangesAsync(); } }
// public async Task UpdateAsync(T entity) { _context.Set<T>().Update(entity); await _context.SaveChangesAsync(); }
// public async Task DeleteAsync(Guid id) { var entity = await _context.Set<T>().FindAsync(id); if (entity != null) { _context.Set<T>().Remove(entity); await _context.SaveChangesAsync(); } }
} }
} }