Files
ShopSolution-backend/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs
Tizian.Breuch 910cab656b AdminShopInfo
2025-09-25 14:47:00 +02:00

71 lines
2.8 KiB
C#

// src/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Shop;
using Webshop.Application.Services.Admin;
namespace Webshop.Api.Controllers.Admin
{
/// <summary>
/// API-Endpunkte zur Verwaltung der zentralen Shop-Stammdaten.
/// </summary>
[ApiController]
[Route("api/v1/admin/[controller]")]
[Authorize(Roles = "Admin")]
public class AdminShopInfoController : ControllerBase
{
private readonly IAdminShopInfoService _shopInfoService;
public AdminShopInfoController(IAdminShopInfoService shopInfoService)
{
_shopInfoService = shopInfoService;
}
/// <summary>
/// Ruft die aktuellen Stammdaten des Shops ab.
/// </summary>
/// <remarks>
/// Diese Daten werden typischerweise in einem "Stammdaten"- oder "Shop-Einstellungen"-Formular im Admin-Dashboard angezeigt.
/// </remarks>
[HttpGet]
[ProducesResponseType(typeof(AdminShopInfoDto), StatusCodes.Status200OK)]
public async Task<IActionResult> GetShopInfo()
{
var result = await _shopInfoService.GetShopInfoAsync();
// GetShopInfoAsync erstellt einen Standardeintrag, wenn keiner vorhanden ist,
// daher sollte es im Normalbetrieb immer erfolgreich sein.
return Ok(result.Value);
}
/// <summary>
/// Aktualisiert die zentralen Stammdaten des Shops.
/// </summary>
/// <remarks>
/// Sendet das gesamte, ausgefüllte DTO. Alle Felder werden überschrieben.
/// </remarks>
/// <param name="shopInfoDto">Das Datenobjekt mit den neuen Shop-Informationen.</param>
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> UpdateShopInfo([FromBody] AdminShopInfoDto shopInfoDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var result = await _shopInfoService.UpdateShopInfoAsync(shopInfoDto);
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." })
};
}
}
}