settings
This commit is contained in:
@@ -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