From bbea2458ae6321e37626971273bda16e4879a061 Mon Sep 17 00:00:00 2001 From: "Tizian.Breuch" Date: Tue, 12 Aug 2025 11:31:25 +0200 Subject: [PATCH] settings --- .../Admin/AdminSettingsController.cs | 33 ++++++++++-- Webshop.Api/Program.cs | 3 ++ Webshop.Api/Webshop.Api.csproj | 4 ++ .../DTOs/Settings/SettingDto.cs | 17 +++--- .../Services/Admin/AdminSettingService.cs | 49 ++++++++++++++--- .../Admin/Interfaces/IAdminSettingService.cs | 13 ++--- .../Interfaces/ISettingRepository.cs | 12 +++-- Webshop.Domain/Interfaces/ISettingService.cs | 12 +++++ .../Repositories/SettingRepository.cs | 38 +++++++++----- .../Services/SettingService.cs | 52 +++++++++++++++++++ 10 files changed, 187 insertions(+), 46 deletions(-) create mode 100644 Webshop.Domain/Interfaces/ISettingService.cs create mode 100644 Webshop.Infrastructure/Services/SettingService.cs diff --git a/Webshop.Api/Controllers/Admin/AdminSettingsController.cs b/Webshop.Api/Controllers/Admin/AdminSettingsController.cs index 12f7c1e..1e88cef 100644 --- a/Webshop.Api/Controllers/Admin/AdminSettingsController.cs +++ b/Webshop.Api/Controllers/Admin/AdminSettingsController.cs @@ -1,10 +1,10 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using Microsoft.AspNetCore.Mvc; +// src/Webshop.Api/Controllers/Admin/AdminSettingsController.cs using Microsoft.AspNetCore.Authorization; - -using System; +using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; +using Webshop.Application.DTOs.Settings; +using Webshop.Application.Services.Admin.Interfaces; namespace Webshop.Api.Controllers.Admin { @@ -13,6 +13,29 @@ namespace Webshop.Api.Controllers.Admin [Authorize(Roles = "Admin")] public class AdminSettingsController : ControllerBase { + private readonly IAdminSettingService _adminSettingService; + public AdminSettingsController(IAdminSettingService adminSettingService) + { + _adminSettingService = adminSettingService; + } + + [HttpGet] + public async Task>>> GetAllSettings() + { + var settings = await _adminSettingService.GetAllGroupedAsync(); + return Ok(settings); + } + + [HttpPut] + public async Task UpdateSettings([FromBody] List settings) + { + if (!ModelState.IsValid) + { + return BadRequest(ModelState); + } + await _adminSettingService.UpdateSettingsAsync(settings); + return NoContent(); + } } -} +} \ No newline at end of file diff --git a/Webshop.Api/Program.cs b/Webshop.Api/Program.cs index 37d4011..980fe29 100644 --- a/Webshop.Api/Program.cs +++ b/Webshop.Api/Program.cs @@ -128,6 +128,9 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); +builder.Services.AddScoped(); + // Controller und API-Infrastruktur builder.Services.AddControllers() diff --git a/Webshop.Api/Webshop.Api.csproj b/Webshop.Api/Webshop.Api.csproj index ac123bd..c0e7068 100644 --- a/Webshop.Api/Webshop.Api.csproj +++ b/Webshop.Api/Webshop.Api.csproj @@ -8,6 +8,10 @@ Linux + + + + diff --git a/Webshop.Application/DTOs/Settings/SettingDto.cs b/Webshop.Application/DTOs/Settings/SettingDto.cs index b2eeb41..828f251 100644 --- a/Webshop.Application/DTOs/Settings/SettingDto.cs +++ b/Webshop.Application/DTOs/Settings/SettingDto.cs @@ -1,16 +1,15 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; -using System.Collections.Generic; -using System.Threading.Tasks; +// src/Webshop.Application/DTOs/Settings/SettingDto.cs +using System.ComponentModel.DataAnnotations; - -namespace Webshop.Application.DTOs +namespace Webshop.Application.DTOs.Settings { public class SettingDto { + [Required] public string Key { get; set; } = string.Empty; - public string Value { get; set; } = string.Empty; + public string? Value { get; set; } public string? Description { get; set; } - public DateTimeOffset LastModifiedDate { get; set; } + public bool IsActive { get; set; } + public string? Group { get; set; } } -} +} \ No newline at end of file diff --git a/Webshop.Application/Services/Admin/AdminSettingService.cs b/Webshop.Application/Services/Admin/AdminSettingService.cs index 053c8b8..f8f4c01 100644 --- a/Webshop.Application/Services/Admin/AdminSettingService.cs +++ b/Webshop.Application/Services/Admin/AdminSettingService.cs @@ -1,18 +1,53 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; +// src/Webshop.Application/Services/Admin/AdminSettingService.cs using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; +using Webshop.Application.DTOs.Settings; using Webshop.Application.Services.Admin.Interfaces; - +using Webshop.Domain.Interfaces; namespace Webshop.Application.Services.Admin { public class AdminSettingService : IAdminSettingService { - // Fügen Sie hier Abhängigkeiten per Dependency Injection hinzu (z.B. Repositories) + private readonly ISettingRepository _settingRepository; - // public AdminSettingService(IYourRepository repository) { } + public AdminSettingService(ISettingRepository settingRepository) + { + _settingRepository = settingRepository; + } - // Fügen Sie hier Service-Methoden hinzu + public async Task>> GetAllGroupedAsync() + { + var settings = await _settingRepository.GetAllAsync(); + + return 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() + ); + } + + public async Task UpdateSettingsAsync(List settings) + { + foreach (var settingDto in settings) + { + var setting = await _settingRepository.GetByKeyAsync(settingDto.Key); + if (setting != null) + { + setting.Value = settingDto.Value; + setting.IsActive = settingDto.IsActive; // Auch IsActive aktualisieren + await _settingRepository.UpdateAsync(setting); + } + } + } } -} +} \ 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 50b9f34..05dc8ad 100644 --- a/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs +++ b/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs @@ -1,16 +1,13 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; +// src/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs using System.Collections.Generic; using System.Threading.Tasks; -using Webshop.Application.DTOs; -using Webshop.Application.DTOs.Auth; -using Webshop.Application.DTOs.Users; - +using Webshop.Application.DTOs.Settings; namespace Webshop.Application.Services.Admin.Interfaces { public interface IAdminSettingService { - // Fügen Sie hier Methodensignaturen hinzu + Task>> GetAllGroupedAsync(); + Task UpdateSettingsAsync(List settings); } -} +} \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/ISettingRepository.cs b/Webshop.Domain/Interfaces/ISettingRepository.cs index d048f8b..d825cad 100644 --- a/Webshop.Domain/Interfaces/ISettingRepository.cs +++ b/Webshop.Domain/Interfaces/ISettingRepository.cs @@ -1,14 +1,16 @@ -// Auto-generiert von CreateWebshopFiles.ps1 -using System; +// src/Webshop.Domain/Interfaces/ISettingRepository.cs using System.Collections.Generic; using System.Threading.Tasks; using Webshop.Domain.Entities; - namespace Webshop.Domain.Interfaces { + // Deklariert reine Datenbankoperationen für die 'Setting'-Entität public interface ISettingRepository { -// Fügen Sie hier Methodensignaturen hinzu + Task> GetAllAsync(); + Task GetByKeyAsync(string key); + Task UpdateAsync(Setting setting); + Task AddAsync(Setting setting); } -} +} \ No newline at end of file diff --git a/Webshop.Domain/Interfaces/ISettingService.cs b/Webshop.Domain/Interfaces/ISettingService.cs new file mode 100644 index 0000000..ce5b95f --- /dev/null +++ b/Webshop.Domain/Interfaces/ISettingService.cs @@ -0,0 +1,12 @@ +// src/Webshop.Domain/Interfaces/ISettingService.cs +using System.Threading.Tasks; + +namespace Webshop.Domain.Interfaces +{ + // Deklariert "Business"-Methoden, die von anderen Services genutzt werden + public interface ISettingService + { + Task GetSettingValueAsync(string key, T defaultValue); + Task UpdateSettingAsync(string key, string? value); + } +} \ No newline at end of file diff --git a/Webshop.Infrastructure/Repositories/SettingRepository.cs b/Webshop.Infrastructure/Repositories/SettingRepository.cs index 0911508..e6db166 100644 --- a/Webshop.Infrastructure/Repositories/SettingRepository.cs +++ b/Webshop.Infrastructure/Repositories/SettingRepository.cs @@ -1,14 +1,14 @@ -// Auto-generiert von CreateWebshopFiles.ps1 +// src/Webshop.Infrastructure/Repositories/SettingRepository.cs using Microsoft.EntityFrameworkCore; +using System.Collections.Generic; +using System.Threading.Tasks; using Webshop.Domain.Entities; using Webshop.Domain.Interfaces; using Webshop.Infrastructure.Data; -using System; -using System.Collections.Generic; -using System.Threading.Tasks; namespace Webshop.Infrastructure.Repositories { + // Implementiert die reinen Datenbankoperationen public class SettingRepository : ISettingRepository { private readonly ApplicationDbContext _context; @@ -18,12 +18,26 @@ namespace Webshop.Infrastructure.Repositories _context = context; } - // Fügen Sie hier Repository-Methoden hinzu - // Beispiel: - // public async Task> GetAllAsync() { return await _context.Set().ToListAsync(); } - // public async Task GetByIdAsync(Guid id) { return await _context.Set().FindAsync(id); } - // public async Task AddAsync(T entity) { _context.Set().Add(entity); await _context.SaveChangesAsync(); } - // public async Task UpdateAsync(T entity) { _context.Set().Update(entity); await _context.SaveChangesAsync(); } - // public async Task DeleteAsync(Guid id) { var entity = await _context.Set().FindAsync(id); if (entity != null) { _context.Set().Remove(entity); await _context.SaveChangesAsync(); } } + public async Task> GetAllAsync() + { + return await _context.Settings.OrderBy(s => s.Group).ThenBy(s => s.Key).ToListAsync(); + } + + public async Task GetByKeyAsync(string key) + { + return await _context.Settings.FirstOrDefaultAsync(s => s.Key == key); + } + + public async Task AddAsync(Setting setting) + { + await _context.Settings.AddAsync(setting); + await _context.SaveChangesAsync(); + } + + public async Task UpdateAsync(Setting setting) + { + _context.Settings.Update(setting); + await _context.SaveChangesAsync(); + } } -} +} \ No newline at end of file diff --git a/Webshop.Infrastructure/Services/SettingService.cs b/Webshop.Infrastructure/Services/SettingService.cs new file mode 100644 index 0000000..a4809be --- /dev/null +++ b/Webshop.Infrastructure/Services/SettingService.cs @@ -0,0 +1,52 @@ +// src/Webshop.Infrastructure/Services/SettingService.cs +using System; +using System.Threading.Tasks; +using Webshop.Domain.Entities; +using Webshop.Domain.Interfaces; + +namespace Webshop.Infrastructure.Services +{ + // Implementiert die "Business"-Methoden + public class SettingService : ISettingService + { + private readonly ISettingRepository _settingRepository; + + public SettingService(ISettingRepository settingRepository) + { + _settingRepository = settingRepository; + } + + public async Task GetSettingValueAsync(string key, T defaultValue) + { + var setting = await _settingRepository.GetByKeyAsync(key); + if (setting == null || !setting.IsActive || string.IsNullOrEmpty(setting.Value)) + { + return defaultValue; + } + + try + { + return (T)Convert.ChangeType(setting.Value, typeof(T)); + } + catch + { + return defaultValue; + } + } + + public async Task UpdateSettingAsync(string key, string? value) + { + var setting = await _settingRepository.GetByKeyAsync(key); + if (setting != null) + { + setting.Value = value; + await _settingRepository.UpdateAsync(setting); + } + else + { + var newSetting = new Setting { Key = key, Value = value, IsActive = true, Group = "General" }; + await _settingRepository.AddAsync(newSetting); + } + } + } +} \ No newline at end of file