adminsettings

This commit is contained in:
Tizian.Breuch
2025-09-25 14:43:47 +02:00
parent e43fdc01fe
commit f5543d35b4
3 changed files with 42 additions and 13 deletions

View File

@@ -1,8 +1,10 @@
// src/Webshop.Api/Controllers/Admin/AdminSettingsController.cs
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Settings;
using Webshop.Application.Services.Admin.Interfaces;
@@ -21,21 +23,31 @@ namespace Webshop.Api.Controllers.Admin
}
[HttpGet]
public async Task<ActionResult<Dictionary<string, List<SettingDto>>>> GetAllSettings()
[ProducesResponseType(typeof(Dictionary<string, List<SettingDto>>), StatusCodes.Status200OK)]
public async Task<IActionResult> GetAllSettings()
{
var settings = await _adminSettingService.GetAllGroupedAsync();
return Ok(settings);
var result = await _adminSettingService.GetAllGroupedAsync();
return Ok(result.Value);
}
[HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateSettings([FromBody] List<SettingDto> settings)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
await _adminSettingService.UpdateSettingsAsync(settings);
return NoContent();
var result = await _adminSettingService.UpdateSettingsAsync(settings);
return result.Type switch
{
ServiceResultType.Success => NoContent(),
ServiceResultType.InvalidInput => BadRequest(new { Message = result.ErrorMessage }),
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
};
}
}
}

View File

@@ -2,6 +2,7 @@
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;
@@ -17,11 +18,11 @@ namespace Webshop.Application.Services.Admin
_settingRepository = settingRepository;
}
public async Task<Dictionary<string, List<SettingDto>>> GetAllGroupedAsync()
public async Task<ServiceResult<Dictionary<string, List<SettingDto>>>> GetAllGroupedAsync()
{
var settings = await _settingRepository.GetAllAsync();
return settings
var groupedSettings = settings
.GroupBy(s => s.Group ?? "Uncategorized")
.ToDictionary(
g => g.Key,
@@ -34,20 +35,35 @@ namespace Webshop.Application.Services.Admin
Group = s.Group
}).ToList()
);
return ServiceResult.Ok(groupedSettings);
}
public async Task UpdateSettingsAsync(List<SettingDto> settings)
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<68>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)
{
var setting = await _settingRepository.GetByKeyAsync(settingDto.Key);
if (setting != null)
if (settingsDict.TryGetValue(settingDto.Key, out var setting))
{
setting.Value = settingDto.Value;
setting.IsActive = settingDto.IsActive; // Auch IsActive aktualisieren
setting.IsActive = settingDto.IsActive;
await _settingRepository.UpdateAsync(setting);
}
}
return ServiceResult.Ok();
}
}
}

View File

@@ -1,13 +1,14 @@
// src/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Settings;
namespace Webshop.Application.Services.Admin.Interfaces
{
public interface IAdminSettingService
{
Task<Dictionary<string, List<SettingDto>>> GetAllGroupedAsync();
Task UpdateSettingsAsync(List<SettingDto> settings);
Task<ServiceResult<Dictionary<string, List<SettingDto>>>> GetAllGroupedAsync();
Task<ServiceResult> UpdateSettingsAsync(List<SettingDto> settings);
}
}