Files
Tizian.Breuch 910cab656b AdminShopInfo
2025-09-25 14:47:00 +02:00

80 lines
3.2 KiB
C#

// 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;
namespace Webshop.Application.Services.Admin
{
public class AdminShopInfoService : IAdminShopInfoService
{
private readonly IShopInfoRepository _shopInfoRepository;
public AdminShopInfoService(IShopInfoRepository shopInfoRepository)
{
_shopInfoRepository = shopInfoRepository;
}
public async Task<ServiceResult<AdminShopInfoDto>> GetShopInfoAsync()
{
var shopInfo = await _shopInfoRepository.GetAsync();
if (shopInfo == null)
{
// Erstelle einen Standardeintrag, falls keiner existiert
shopInfo = new ShopInfo { Id = Guid.NewGuid(), ShopName = "Mein Webshop", ContactEmail = "admin@example.com" };
await _shopInfoRepository.AddAsync(shopInfo);
}
var dto = new AdminShopInfoDto
{
ShopName = shopInfo.ShopName,
Slogan = shopInfo.Slogan,
ContactEmail = shopInfo.ContactEmail,
PhoneNumber = shopInfo.PhoneNumber,
Street = shopInfo.Street,
City = shopInfo.City,
PostalCode = shopInfo.PostalCode,
Country = shopInfo.Country,
VatNumber = shopInfo.VatNumber,
CompanyRegistrationNumber = shopInfo.CompanyRegistrationNumber,
FacebookUrl = shopInfo.FacebookUrl,
InstagramUrl = shopInfo.InstagramUrl,
TwitterUrl = shopInfo.TwitterUrl
};
return ServiceResult.Ok(dto);
}
public async Task<ServiceResult> UpdateShopInfoAsync(AdminShopInfoDto shopInfoDto)
{
var shopInfo = await _shopInfoRepository.GetAsync();
if (shopInfo == null)
{
// 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
shopInfo.ShopName = shopInfoDto.ShopName;
shopInfo.Slogan = shopInfoDto.Slogan;
shopInfo.ContactEmail = shopInfoDto.ContactEmail;
shopInfo.PhoneNumber = shopInfoDto.PhoneNumber;
shopInfo.Street = shopInfoDto.Street;
shopInfo.City = shopInfoDto.City;
shopInfo.PostalCode = shopInfoDto.PostalCode;
shopInfo.Country = shopInfoDto.Country;
shopInfo.VatNumber = shopInfoDto.VatNumber;
shopInfo.CompanyRegistrationNumber = shopInfoDto.CompanyRegistrationNumber;
shopInfo.FacebookUrl = shopInfoDto.FacebookUrl;
shopInfo.InstagramUrl = shopInfoDto.InstagramUrl;
shopInfo.TwitterUrl = shopInfoDto.TwitterUrl;
await _shopInfoRepository.UpdateAsync(shopInfo);
return ServiceResult.Ok();
}
}
}