AdminShopInfo

This commit is contained in:
Tizian.Breuch
2025-09-25 14:47:00 +02:00
parent 8f222ef4aa
commit 910cab656b
3 changed files with 33 additions and 11 deletions

View File

@@ -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.
/// </remarks>
[HttpGet]
public async Task<ActionResult<AdminShopInfoDto>> GetShopInfo()
[ProducesResponseType(typeof(AdminShopInfoDto), StatusCodes.Status200OK)]
public async Task<IActionResult> 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);
}
/// <summary>
@@ -43,14 +48,24 @@ namespace Webshop.Api.Controllers.Admin
/// </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);
}
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." })
};
}
}
}

View File

@@ -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<AdminShopInfoDto> GetShopInfoAsync()
public async Task<ServiceResult<AdminShopInfoDto>> 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<ServiceResult> 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();
}
}
}

View File

@@ -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<AdminShopInfoDto> GetShopInfoAsync();
Task UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto);
Task<ServiceResult<AdminShopInfoDto>> GetShopInfoAsync();
Task<ServiceResult> UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto);
}
}