This commit is contained in:
Tizian.Breuch
2025-08-12 11:31:25 +02:00
parent 1692fe198e
commit bbea2458ae
10 changed files with 187 additions and 46 deletions

View File

@@ -1,10 +1,10 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Api/Controllers/Admin/AdminSettingsController.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Settings;
using Webshop.Application.Services.Admin.Interfaces;
namespace Webshop.Api.Controllers.Admin namespace Webshop.Api.Controllers.Admin
{ {
@@ -13,6 +13,29 @@ namespace Webshop.Api.Controllers.Admin
[Authorize(Roles = "Admin")] [Authorize(Roles = "Admin")]
public class AdminSettingsController : ControllerBase public class AdminSettingsController : ControllerBase
{ {
private readonly IAdminSettingService _adminSettingService;
public AdminSettingsController(IAdminSettingService adminSettingService)
{
_adminSettingService = adminSettingService;
}
[HttpGet]
public async Task<ActionResult<Dictionary<string, List<SettingDto>>>> GetAllSettings()
{
var settings = await _adminSettingService.GetAllGroupedAsync();
return Ok(settings);
}
[HttpPut]
public async Task<IActionResult> UpdateSettings([FromBody] List<SettingDto> settings)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
await _adminSettingService.UpdateSettingsAsync(settings);
return NoContent();
}
} }
} }

View File

@@ -128,6 +128,9 @@ builder.Services.AddScoped<ICheckoutService, CheckoutService>();
builder.Services.AddScoped<IReviewService, ReviewService>(); builder.Services.AddScoped<IReviewService, ReviewService>();
builder.Services.AddScoped<IAdminShopInfoService, AdminShopInfoService>(); builder.Services.AddScoped<IAdminShopInfoService, AdminShopInfoService>();
builder.Services.AddScoped<IShopInfoService, ShopInfoService>(); builder.Services.AddScoped<IShopInfoService, ShopInfoService>();
builder.Services.AddScoped<ISettingService, SettingService>();
builder.Services.AddScoped<IAdminSettingService, AdminSettingService>();
// Controller und API-Infrastruktur // Controller und API-Infrastruktur
builder.Services.AddControllers() builder.Services.AddControllers()

View File

@@ -8,6 +8,10 @@
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> <DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Content Include="Properties\launchSettings.json" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.18" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.18" />

View File

@@ -1,16 +1,15 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Application/DTOs/Settings/SettingDto.cs
using System; using System.ComponentModel.DataAnnotations;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Webshop.Application.DTOs.Settings
namespace Webshop.Application.DTOs
{ {
public class SettingDto public class SettingDto
{ {
[Required]
public string Key { get; set; } = string.Empty; 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 string? Description { get; set; }
public DateTimeOffset LastModifiedDate { get; set; } public bool IsActive { get; set; }
public string? Group { get; set; }
} }
} }

View File

@@ -1,18 +1,53 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Application/Services/Admin/AdminSettingService.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs.Settings;
using Webshop.Application.Services.Admin.Interfaces; using Webshop.Application.Services.Admin.Interfaces;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Admin namespace Webshop.Application.Services.Admin
{ {
public class AdminSettingService : IAdminSettingService 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<Dictionary<string, List<SettingDto>>> 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<SettingDto> 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);
}
}
}
} }
} }

View File

@@ -1,16 +1,13 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Application/Services/Admin/Interfaces/IAdminSettingService.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Application.DTOs; using Webshop.Application.DTOs.Settings;
using Webshop.Application.DTOs.Auth;
using Webshop.Application.DTOs.Users;
namespace Webshop.Application.Services.Admin.Interfaces namespace Webshop.Application.Services.Admin.Interfaces
{ {
public interface IAdminSettingService public interface IAdminSettingService
{ {
// Fügen Sie hier Methodensignaturen hinzu Task<Dictionary<string, List<SettingDto>>> GetAllGroupedAsync();
Task UpdateSettingsAsync(List<SettingDto> settings);
} }
} }

View File

@@ -1,14 +1,16 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Domain/Interfaces/ISettingRepository.cs
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
namespace Webshop.Domain.Interfaces namespace Webshop.Domain.Interfaces
{ {
// Deklariert reine Datenbankoperationen für die 'Setting'-Entität
public interface ISettingRepository public interface ISettingRepository
{ {
// Fügen Sie hier Methodensignaturen hinzu Task<IEnumerable<Setting>> GetAllAsync();
Task<Setting?> GetByKeyAsync(string key);
Task UpdateAsync(Setting setting);
Task AddAsync(Setting setting);
} }
} }

View File

@@ -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<T> GetSettingValueAsync<T>(string key, T defaultValue);
Task UpdateSettingAsync(string key, string? value);
}
}

View File

@@ -1,14 +1,14 @@
// Auto-generiert von CreateWebshopFiles.ps1 // src/Webshop.Infrastructure/Repositories/SettingRepository.cs
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Domain.Entities; using Webshop.Domain.Entities;
using Webshop.Domain.Interfaces; using Webshop.Domain.Interfaces;
using Webshop.Infrastructure.Data; using Webshop.Infrastructure.Data;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Webshop.Infrastructure.Repositories namespace Webshop.Infrastructure.Repositories
{ {
// Implementiert die reinen Datenbankoperationen
public class SettingRepository : ISettingRepository public class SettingRepository : ISettingRepository
{ {
private readonly ApplicationDbContext _context; private readonly ApplicationDbContext _context;
@@ -18,12 +18,26 @@ namespace Webshop.Infrastructure.Repositories
_context = context; _context = context;
} }
// Fügen Sie hier Repository-Methoden hinzu public async Task<IEnumerable<Setting>> GetAllAsync()
// Beispiel: {
// public async Task<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().ToListAsync(); } return await _context.Settings.OrderBy(s => s.Group).ThenBy(s => s.Key).ToListAsync();
// public async Task<T?> GetByIdAsync(Guid id) { return await _context.Set<T>().FindAsync(id); } }
// public async Task AddAsync(T entity) { _context.Set<T>().Add(entity); await _context.SaveChangesAsync(); }
// public async Task UpdateAsync(T entity) { _context.Set<T>().Update(entity); await _context.SaveChangesAsync(); } public async Task<Setting?> GetByKeyAsync(string key)
// public async Task DeleteAsync(Guid id) { var entity = await _context.Set<T>().FindAsync(id); if (entity != null) { _context.Set<T>().Remove(entity); await _context.SaveChangesAsync(); } } {
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();
}
} }
} }

View File

@@ -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<T> GetSettingValueAsync<T>(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);
}
}
}
}