Split Geekbot.net into src/Bot, src/Core, and src/Web
This commit is contained in:
parent
7b6dd2d2f9
commit
fc0af492ad
197 changed files with 542 additions and 498 deletions
68
src/Core/GlobalSettings/GlobalSettings.cs
Normal file
68
src/Core/GlobalSettings/GlobalSettings.cs
Normal file
|
@ -0,0 +1,68 @@
|
|||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Database.Models;
|
||||
|
||||
namespace Geekbot.Core.GlobalSettings
|
||||
{
|
||||
public class GlobalSettings : IGlobalSettings
|
||||
{
|
||||
private readonly DatabaseContext _database;
|
||||
private readonly Dictionary<string, string> _cache;
|
||||
|
||||
public GlobalSettings(DatabaseContext database)
|
||||
{
|
||||
_database = database;
|
||||
_cache = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public async Task<bool> SetKey(string keyName, string value)
|
||||
{
|
||||
try
|
||||
{
|
||||
var key = GetKeyFull(keyName);
|
||||
if (key == null)
|
||||
{
|
||||
_database.Globals.Add(new GlobalsModel()
|
||||
{
|
||||
Name = keyName,
|
||||
Value = value
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
key.Value = value;
|
||||
_database.Globals.Update(key);
|
||||
_cache[keyName] = value;
|
||||
await _database.SaveChangesAsync();
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public string GetKey(string keyName)
|
||||
{
|
||||
var keyValue = "";
|
||||
if (string.IsNullOrEmpty(_cache.GetValueOrDefault(keyName)))
|
||||
{
|
||||
keyValue = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName))?.Value ?? string.Empty;
|
||||
_cache[keyName] = keyValue;
|
||||
}
|
||||
else
|
||||
{
|
||||
keyValue = _cache[keyName];
|
||||
}
|
||||
return keyValue ;
|
||||
}
|
||||
|
||||
public GlobalsModel GetKeyFull(string keyName)
|
||||
{
|
||||
var key = _database.Globals.FirstOrDefault(k => k.Name.Equals(keyName));
|
||||
return key;
|
||||
}
|
||||
}
|
||||
}
|
12
src/Core/GlobalSettings/IGlobalSettings.cs
Normal file
12
src/Core/GlobalSettings/IGlobalSettings.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Threading.Tasks;
|
||||
using Geekbot.Core.Database.Models;
|
||||
|
||||
namespace Geekbot.Core.GlobalSettings
|
||||
{
|
||||
public interface IGlobalSettings
|
||||
{
|
||||
Task<bool> SetKey(string keyName, string value);
|
||||
string GetKey(string keyName);
|
||||
GlobalsModel GetKeyFull(string keyName);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue