feat: premium ID changes
Some checks failed
Build and push Docker image / .net docker build (push) Has been cancelled
.net checks / run .net tests (push) Has been cancelled
.net checks / dotnet-format (push) Has been cancelled
Build and push Rust service Docker images / rust docker build (push) Has been cancelled
rust checks / cargo fmt (push) Has been cancelled

This commit is contained in:
Iris System 2025-12-28 00:06:51 +13:00
parent 84e98450e0
commit def9285250
19 changed files with 364 additions and 5 deletions

View file

@ -0,0 +1,34 @@
using Dapper;
using SqlKata;
using NodaTime;
namespace PluralKit.Core;
public partial class ModelRepository
{
public Task<HidChangelog?> GetHidChangelogById(int id)
{
var query = new Query("hid_changelog").Where("id", id);
return _db.QueryFirst<HidChangelog?>(query);
}
public async Task<HidChangelog> CreateHidChangelog(SystemId system, ulong discord_uid, string hid_type, string hid_old, string hid_new, IPKConnection? conn = null)
{
var query = new Query("hid_changelog").AsInsert(new { system, discord_uid, hid_type, hid_old, hid_new, });
var changelog = await _db.QueryFirst<HidChangelog>(conn, query, "returning *");
_logger.Information("Created HidChangelog {HidChangelogId} for system {SystemId}: {HidType} {OldHid} -> {NewHid}", changelog.Id, system, hid_type, hid_old, hid_new);
return changelog;
}
public Task<int> GetHidChangelogCountForDate(SystemId system, LocalDate date)
{
var query = new Query("hid_changelog")
.SelectRaw("count(*)")
.Where("system", system)
.WhereDate("created", date);
return _db.QueryFirst<int>(query);
}
}

View file

@ -0,0 +1,49 @@
using SqlKata;
using Npgsql;
namespace PluralKit.Core;
public partial class ModelRepository
{
public Task<PremiumAllowance?> GetPremiumAllowance(SystemId system, IPKConnection conn = null)
=> _db.QueryFirst<PremiumAllowance?>(conn, new Query("premium_allowances").Where("system", system));
public Task CreatePremiumAllowance(SystemId system, IPKConnection conn = null)
{
var query = new Query("premium_allowances").AsInsert(new
{
system = system,
});
return _db.ExecuteQuery(query, "on conflict do nothing");
}
public async Task<PremiumAllowance> UpdatePremiumAllowance(SystemId system, PremiumAllowancePatch patch, IPKConnection conn = null)
{
var query = patch.Apply(new Query("premium_allowances").Where("system", system));
return await _db.QueryFirst<PremiumAllowance>(conn, query, "returning *");
}
public async Task<bool> UpdatePremiumAllowanceForIdChange(SystemId system, IPKConnection conn = null)
{
var query = new Query("premium_allowances")
.AsUpdate(new
{
id_changes_remaining = new UnsafeLiteral("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;
}
}

View file

@ -1,4 +1,5 @@
using SqlKata;
using Npgsql;
namespace PluralKit.Core;