Files
ShopSolution-backend/Webshop.Api/Controllers/Admin/AdminPaymentMethodsController.cs
2025-09-25 14:17:42 +02:00

110 lines
4.8 KiB
C#

// 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;
using Webshop.Application.DTOs.Payments;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
[ApiController]
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminPaymentMethodsController : ControllerBase
{
private readonly IAdminPaymentMethodService _adminPaymentMethodService;
public AdminPaymentMethodsController(IAdminPaymentMethodService adminPaymentMethodService)
{
_adminPaymentMethodService = adminPaymentMethodService;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<AdminPaymentMethodDto>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllPaymentMethods()
{
var result = await _adminPaymentMethodService.GetAllAsync();
return Ok(result.Value);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(AdminPaymentMethodDto), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> GetPaymentMethodById(Guid id)
{
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]
[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 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(new { Message = "ID in der URL und im Body stimmen nicht überein." });
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
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 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." })
};
}
}
}