69 lines
2.5 KiB
C#
69 lines
2.5 KiB
C#
// src/Webshop.Application/Services/Admin/AdminSettingService.cs
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Webshop.Application;
|
|
using Webshop.Application.DTOs.Settings;
|
|
using Webshop.Application.Services.Admin.Interfaces;
|
|
using Webshop.Domain.Interfaces;
|
|
|
|
namespace Webshop.Application.Services.Admin
|
|
{
|
|
public class AdminSettingService : IAdminSettingService
|
|
{
|
|
private readonly ISettingRepository _settingRepository;
|
|
|
|
public AdminSettingService(ISettingRepository settingRepository)
|
|
{
|
|
_settingRepository = settingRepository;
|
|
}
|
|
|
|
public async Task<ServiceResult<Dictionary<string, List<SettingDto>>>> GetAllGroupedAsync()
|
|
{
|
|
var settings = await _settingRepository.GetAllAsync();
|
|
|
|
var groupedSettings = settings
|
|
.GroupBy(s => s.Group ?? "Uncategorized")
|
|
.ToDictionary(
|
|
g => g.Key,
|
|
g => g.Select(s => new SettingDto
|
|
{
|
|
Key = s.Key,
|
|
Value = s.Value,
|
|
Description = s.Description,
|
|
IsActive = s.IsActive,
|
|
Group = s.Group
|
|
}).ToList()
|
|
);
|
|
|
|
return ServiceResult.Ok(groupedSettings);
|
|
}
|
|
|
|
public async Task<ServiceResult> UpdateSettingsAsync(List<SettingDto> settings)
|
|
{
|
|
var existingSettings = await _settingRepository.GetAllAsync();
|
|
var existingKeys = existingSettings.Select(s => s.Key).ToHashSet();
|
|
|
|
var invalidKeys = settings.Where(s => !existingKeys.Contains(s.Key)).Select(s => s.Key).ToList();
|
|
if (invalidKeys.Any())
|
|
{
|
|
return ServiceResult.Fail(ServiceResultType.InvalidInput, $"Die folgenden Einstellungsschlüssel wurden nicht gefunden: {string.Join(", ", invalidKeys)}");
|
|
}
|
|
|
|
// Umwandlung in ein Dictionary für effizienten Zugriff
|
|
var settingsDict = existingSettings.ToDictionary(s => s.Key);
|
|
|
|
foreach (var settingDto in settings)
|
|
{
|
|
if (settingsDict.TryGetValue(settingDto.Key, out var setting))
|
|
{
|
|
setting.Value = settingDto.Value;
|
|
setting.IsActive = settingDto.IsActive;
|
|
await _settingRepository.UpdateAsync(setting);
|
|
}
|
|
}
|
|
|
|
return ServiceResult.Ok();
|
|
}
|
|
}
|
|
} |