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 // src/Webshop.Api/Controllers/Admin/AdminSettingsController.cs
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Settings; using Webshop.Application.DTOs.Settings;
using Webshop.Application.Services.Admin.Interfaces; using Webshop.Application.Services.Admin.Interfaces;
@@ -21,21 +23,31 @@ namespace Webshop.Api.Controllers.Admin
} }
[HttpGet] [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(); var result = await _adminSettingService.GetAllGroupedAsync();
return Ok(settings); return Ok(result.Value);
} }
[HttpPut] [HttpPut]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(typeof(ValidationProblemDetails), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> UpdateSettings([FromBody] List<SettingDto> settings) public async Task<IActionResult> UpdateSettings([FromBody] List<SettingDto> settings)
{ {
if (!ModelState.IsValid) if (!ModelState.IsValid)
{ {
return BadRequest(ModelState); 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.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Settings; using Webshop.Application.DTOs.Settings;
using Webshop.Application.Services.Admin.Interfaces; using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Domain.Interfaces; using Webshop.Domain.Interfaces;
@@ -17,11 +18,11 @@ namespace Webshop.Application.Services.Admin
_settingRepository = settingRepository; _settingRepository = settingRepository;
} }
public async Task<Dictionary<string, List<SettingDto>>> GetAllGroupedAsync() public async Task<ServiceResult<Dictionary<string, List<SettingDto>>>> GetAllGroupedAsync()
{ {
var settings = await _settingRepository.GetAllAsync(); var settings = await _settingRepository.GetAllAsync();
return settings var groupedSettings = settings
.GroupBy(s => s.Group ?? "Uncategorized") .GroupBy(s => s.Group ?? "Uncategorized")
.ToDictionary( .ToDictionary(
g => g.Key, g => g.Key,
@@ -34,20 +35,35 @@ namespace Webshop.Application.Services.Admin
Group = s.Group Group = s.Group
}).ToList() }).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) foreach (var settingDto in settings)
{ {
var setting = await _settingRepository.GetByKeyAsync(settingDto.Key); if (settingsDict.TryGetValue(settingDto.Key, out var setting))
if (setting != null)
{ {
setting.Value = settingDto.Value; setting.Value = settingDto.Value;
setting.IsActive = settingDto.IsActive; // Auch IsActive aktualisieren setting.IsActive = settingDto.IsActive;
await _settingRepository.UpdateAsync(setting); await _settingRepository.UpdateAsync(setting);
} }
} }
return ServiceResult.Ok();
} }
} }
} }

View File

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