settings
This commit is contained in:
@@ -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<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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,9 @@ builder.Services.AddScoped<ICheckoutService, CheckoutService>();
|
||||
builder.Services.AddScoped<IReviewService, ReviewService>();
|
||||
builder.Services.AddScoped<IAdminShopInfoService, AdminShopInfoService>();
|
||||
builder.Services.AddScoped<IShopInfoService, ShopInfoService>();
|
||||
builder.Services.AddScoped<ISettingService, SettingService>();
|
||||
builder.Services.AddScoped<IAdminSettingService, AdminSettingService>();
|
||||
|
||||
|
||||
// Controller und API-Infrastruktur
|
||||
builder.Services.AddControllers()
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Content Include="Properties\launchSettings.json" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.18" />
|
||||
|
||||
@@ -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; }
|
||||
}
|
||||
}
|
||||
@@ -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<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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<Dictionary<string, List<SettingDto>>> GetAllGroupedAsync();
|
||||
Task UpdateSettingsAsync(List<SettingDto> settings);
|
||||
}
|
||||
}
|
||||
@@ -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<IEnumerable<Setting>> GetAllAsync();
|
||||
Task<Setting?> GetByKeyAsync(string key);
|
||||
Task UpdateAsync(Setting setting);
|
||||
Task AddAsync(Setting setting);
|
||||
}
|
||||
}
|
||||
12
Webshop.Domain/Interfaces/ISettingService.cs
Normal file
12
Webshop.Domain/Interfaces/ISettingService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -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<IEnumerable<T>> GetAllAsync() { return await _context.Set<T>().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 DeleteAsync(Guid id) { var entity = await _context.Set<T>().FindAsync(id); if (entity != null) { _context.Set<T>().Remove(entity); await _context.SaveChangesAsync(); } }
|
||||
public async Task<IEnumerable<Setting>> GetAllAsync()
|
||||
{
|
||||
return await _context.Settings.OrderBy(s => s.Group).ThenBy(s => s.Key).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Setting?> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Webshop.Infrastructure/Services/SettingService.cs
Normal file
52
Webshop.Infrastructure/Services/SettingService.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user