PluralKit/PluralKit.Core/Database/Repository/ModelRepository.SystemConfig.cs

47 lines
1.4 KiB
C#
Raw Normal View History

2021-11-29 21:35:21 -05:00
using SqlKata;
2025-12-28 00:06:51 +13:00
using Npgsql;
2021-11-29 21:35:21 -05:00
namespace PluralKit.Core;
public partial class ModelRepository
{
2021-11-30 17:04:42 -05:00
public Task<SystemConfig> GetSystemConfig(SystemId system, IPKConnection conn = null)
=> _db.QueryFirst<SystemConfig>(conn, new Query("system_config").Where("system", system));
2021-11-29 21:35:21 -05:00
2021-11-30 17:04:42 -05:00
public async Task<SystemConfig> UpdateSystemConfig(SystemId system, SystemConfigPatch patch, IPKConnection conn = null)
2021-11-29 21:35:21 -05:00
{
var query = patch.Apply(new Query("system_config").Where("system", system));
2021-11-30 17:04:42 -05:00
var config = await _db.QueryFirst<SystemConfig>(conn, query, "returning *");
2021-11-29 21:35:21 -05:00
_ = _dispatch.Dispatch(system, new UpdateDispatchData
{
Event = DispatchEvent.UPDATE_SETTINGS,
EventData = patch.ToJson()
});
return config;
}
2025-12-28 00:06:51 +13:00
public async Task<bool> TryUpdateSystemConfigForIdChange(SystemId system, IPKConnection conn = null)
{
var query = new Query("system_config")
.AsUpdate(new
{
premium_id_changes_remaining = new UnsafeLiteral("premium_id_changes_remaining - 1")
})
.Where("system", system);
try
{
await _db.ExecuteQuery(conn, query);
}
catch (PostgresException pe)
{
if (!pe.Message.Contains("violates check constraint"))
throw;
return false;
}
return true;
}
2021-11-29 21:35:21 -05:00
}