You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.5 KiB
C#
140 lines
4.5 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Haoliang.Models.System;
|
|
using Haoliang.Data.Repositories;
|
|
|
|
namespace Haoliang.Data.Repositories
|
|
{
|
|
public interface ISystemConfigRepository : IRepository<SystemConfig>
|
|
{
|
|
Task<SystemConfig> GetByKeyAsync(string configKey);
|
|
Task<bool> DeleteByKeyAsync(string configKey);
|
|
Task<bool> KeyExistsAsync(string configKey);
|
|
Task<IEnumerable<SystemConfig>> GetByCategoryAsync(string category);
|
|
SystemConfig UpsertAsync(SystemConfig config);
|
|
Task<string> GetValueAsync(string configKey);
|
|
Task<bool> SetValueAsync(string configKey, string value);
|
|
Task<IEnumerable<SystemConfig>> GetActiveConfigsAsync();
|
|
Task<SystemConfig> GetDefaultConfigAsync();
|
|
Task<bool> UpdateConfigValueAsync(string configKey, string value);
|
|
}
|
|
|
|
public class SystemConfigRepository : Repository<SystemConfig>, ISystemConfigRepository
|
|
{
|
|
private readonly CNCDbContext _context;
|
|
|
|
public SystemConfigRepository(CNCDbContext context) : base(context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<SystemConfig> GetByKeyAsync(string configKey)
|
|
{
|
|
return await _context.SystemConfigs
|
|
.FirstOrDefaultAsync(c => c.ConfigKey == configKey);
|
|
}
|
|
|
|
public async Task<bool> DeleteByKeyAsync(string configKey)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
if (config != null)
|
|
{
|
|
_context.SystemConfigs.Remove(config);
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<bool> KeyExistsAsync(string configKey)
|
|
{
|
|
return await _context.SystemConfigs
|
|
.AnyAsync(c => c.ConfigKey == configKey);
|
|
}
|
|
|
|
public async Task<IEnumerable<SystemConfig>> GetByCategoryAsync(string category)
|
|
{
|
|
return await _context.SystemConfigs
|
|
.Where(c => c.Category == category)
|
|
.OrderBy(c => c.ConfigKey)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public SystemConfig UpsertAsync(SystemConfig config)
|
|
{
|
|
var existing = _context.SystemConfigs
|
|
.FirstOrDefault(c => c.ConfigKey == config.ConfigKey);
|
|
|
|
if (existing != null)
|
|
{
|
|
// Update existing
|
|
existing.ConfigValue = config.ConfigValue;
|
|
existing.Description = config.Description;
|
|
existing.UpdatedAt = DateTime.Now;
|
|
existing.IsActive = config.IsActive;
|
|
existing.Category = config.Category;
|
|
_context.SystemConfigs.Update(existing);
|
|
}
|
|
else
|
|
{
|
|
// Insert new
|
|
config.CreatedAt = DateTime.Now;
|
|
config.UpdatedAt = DateTime.Now;
|
|
_context.SystemConfigs.Add(config);
|
|
}
|
|
|
|
SaveAsync();
|
|
return config;
|
|
}
|
|
|
|
public async Task<string> GetValueAsync(string configKey)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
return config?.ConfigValue;
|
|
}
|
|
|
|
public async Task<bool> SetValueAsync(string configKey, string value)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
if (config != null)
|
|
{
|
|
config.ConfigValue = value;
|
|
config.UpdatedAt = DateTime.Now;
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public async Task<IEnumerable<SystemConfig>> GetActiveConfigsAsync()
|
|
{
|
|
return await _context.SystemConfigs
|
|
.Where(c => c.IsActive)
|
|
.OrderBy(c => c.Category)
|
|
.ThenBy(c => c.ConfigKey)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<SystemConfig> GetDefaultConfigAsync()
|
|
{
|
|
return await _context.SystemConfigs
|
|
.FirstOrDefaultAsync(c => c.IsDefault);
|
|
}
|
|
|
|
public async Task<bool> UpdateConfigValueAsync(string configKey, string value)
|
|
{
|
|
var config = await GetByKeyAsync(configKey);
|
|
if (config != null)
|
|
{
|
|
config.ConfigValue = value;
|
|
config.UpdatedAt = DateTime.Now;
|
|
await SaveAsync();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
} |