From 910cab656bc9e7465053155809d029830636aca0 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Thu, 25 Sep 2025 14:47:00 +0200 Subject: [PATCH] AdminShopInfo --- .../Admin/AdminShopInfoController.cs | 25 +++++++++++++++---- .../Services/Admin/AdminShopInfoService.cs | 14 ++++++++--- .../Admin/Interfaces/IAdminShopInfoService.cs | 5 ++-- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs b/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs index ce3284f..10b1ce2 100644 --- a/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs +++ b/Webshop.Api/Controllers/Admin/AdminShopInfoController.cs @@ -1,7 +1,9 @@ // 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; @@ -29,10 +31,13 @@ namespace Webshop.Api.Controllers.Admin /// Diese Daten werden typischerweise in einem "Stammdaten"- oder "Shop-Einstellungen"-Formular im Admin-Dashboard angezeigt. /// [HttpGet] - public async Task> GetShopInfo() + [ProducesResponseType(typeof(AdminShopInfoDto), StatusCodes.Status200OK)] + public async Task GetShopInfo() { - var info = await _shopInfoService.GetShopInfoAsync(); - return Ok(info); + 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); } /// @@ -43,14 +48,24 @@ namespace Webshop.Api.Controllers.Admin /// /// Das Datenobjekt mit den neuen Shop-Informationen. [HttpPut] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)] + [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] public async Task UpdateShopInfo([FromBody] AdminShopInfoDto shopInfoDto) { if (!ModelState.IsValid) { return BadRequest(ModelState); } - await _shopInfoService.UpdateShopInfoAsync(shopInfoDto); - return NoContent(); + + 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." }) + }; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/AdminShopInfoService.cs b/Webshop.Application/Services/Admin/AdminShopInfoService.cs index d744a09..8260f39 100644 --- a/Webshop.Application/Services/Admin/AdminShopInfoService.cs +++ b/Webshop.Application/Services/Admin/AdminShopInfoService.cs @@ -1,6 +1,7 @@ // src/Webshop.Application/Services/Admin/AdminShopInfoService.cs using System; using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs.Shop; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; @@ -16,7 +17,7 @@ namespace Webshop.Application.Services.Admin _shopInfoRepository = shopInfoRepository; } - public async Task GetShopInfoAsync() + public async Task> GetShopInfoAsync() { var shopInfo = await _shopInfoRepository.GetAsync(); @@ -27,7 +28,7 @@ namespace Webshop.Application.Services.Admin await _shopInfoRepository.AddAsync(shopInfo); } - return new AdminShopInfoDto + var dto = new AdminShopInfoDto { ShopName = shopInfo.ShopName, Slogan = shopInfo.Slogan, @@ -43,14 +44,18 @@ namespace Webshop.Application.Services.Admin InstagramUrl = shopInfo.InstagramUrl, TwitterUrl = shopInfo.TwitterUrl }; + + return ServiceResult.Ok(dto); } - public async Task UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto) + public async Task UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto) { var shopInfo = await _shopInfoRepository.GetAsync(); if (shopInfo == null) { - throw new InvalidOperationException("ShopInfo entity does not exist and cannot be updated."); + // Dieser Fall sollte theoretisch nie eintreten, da GetShopInfoAsync die Entität erstellt. + // Zur Sicherheit fangen wir ihn trotzdem ab. + return ServiceResult.Fail(ServiceResultType.NotFound, "ShopInfo-Entität existiert nicht und kann nicht aktualisiert werden."); } // Mappe die Werte vom DTO auf die Entität @@ -69,6 +74,7 @@ namespace Webshop.Application.Services.Admin shopInfo.TwitterUrl = shopInfoDto.TwitterUrl; await _shopInfoRepository.UpdateAsync(shopInfo); + return ServiceResult.Ok(); } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/Interfaces/IAdminShopInfoService.cs b/Webshop.Application/Services/Admin/Interfaces/IAdminShopInfoService.cs index eb79df3..c4eb4c0 100644 --- a/Webshop.Application/Services/Admin/Interfaces/IAdminShopInfoService.cs +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminShopInfoService.cs @@ -1,12 +1,13 @@ // src/Webshop.Application/Services/Admin/IAdminShopInfoService.cs using System.Threading.Tasks; +using Webshop.Application; using Webshop.Application.DTOs.Shop; namespace Webshop.Application.Services.Admin { public interface IAdminShopInfoService { - Task GetShopInfoAsync(); - Task UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto); + Task> GetShopInfoAsync(); + Task UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto); } } \ No newline at end of file