diff --git a/Webshop.Api/Controllers/Admin/AdminSettingsController.cs b/Webshop.Api/Controllers/Admin/AdminSettingsController.cs index 1e88cef..2dbd0da 100644 --- a/Webshop.Api/Controllers/Admin/AdminSettingsController.cs +++ b/Webshop.Api/Controllers/Admin/AdminSettingsController.cs @@ -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>>> GetAllSettings() + [ProducesResponseType(typeof(Dictionary>), StatusCodes.Status200OK)] + public async Task 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 UpdateSettings([FromBody] List 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." }) + }; } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/AdminSettingService.cs b/Webshop.Application/Services/Admin/AdminSettingService.cs index f8f4c01..3d4202f 100644 --- a/Webshop.Application/Services/Admin/AdminSettingService.cs +++ b/Webshop.Application/Services/Admin/AdminSettingService.cs @@ -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>> GetAllGroupedAsync() + public async Task>>> 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 settings) + public async Task UpdateSettingsAsync(List 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) { - 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(); } } } \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs b/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs index 05dc8ad..7c27d5b 100644 --- a/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs @@ -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>> GetAllGroupedAsync(); - Task UpdateSettingsAsync(List settings); + Task>>> GetAllGroupedAsync(); + Task UpdateSettingsAsync(List settings); } } \ No newline at end of file