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

66 lines
2.7 KiB
C#
Raw Normal View History

using SqlKata;
2020-08-29 11:46:27 +00:00
namespace PluralKit.Core;
public partial class ModelRepository
2020-08-29 11:46:27 +00:00
{
public Task<GuildConfig> GetGuild(ulong guild)
2020-08-29 11:46:27 +00:00
{
var query = new Query("servers").AsInsert(new { id = guild });
// sqlkata doesn't support postgres on conflict, so we just hack it on here
return _db.QueryFirst<GuildConfig>(query, "on conflict (id) do update set id = @$1 returning *");
}
2021-08-27 15:03:47 +00:00
public Task UpdateGuild(ulong guild, GuildPatch patch)
{
_logger.Information("Updated guild {GuildId}: {@GuildPatch}", guild, patch);
var query = patch.Apply(new Query("servers").Where("id", guild));
return _db.ExecuteQuery(query, "returning *");
}
2021-10-13 05:02:34 +00:00
public Task<SystemGuildSettings> GetSystemGuild(ulong guild, SystemId system, bool defaultInsert = true)
{
if (!defaultInsert)
return _db.QueryFirst<SystemGuildSettings>(new Query("system_guild")
.Where("guild", guild)
.Where("system", system)
);
2020-08-29 11:46:27 +00:00
var query = new Query("system_guild").AsInsert(new { guild, system });
return _db.QueryFirst<SystemGuildSettings>(query,
"on conflict (guild, system) do update set guild = $1, system = $2 returning *"
);
}
2021-08-27 15:03:47 +00:00
public async Task<SystemGuildSettings> UpdateSystemGuild(SystemId system, ulong guild, SystemGuildPatch patch)
{
_logger.Information("Updated {SystemId} in guild {GuildId}: {@SystemGuildPatch}", system, guild, patch);
var query = patch.Apply(new Query("system_guild").Where("system", system).Where("guild", guild));
var settings = await _db.QueryFirst<SystemGuildSettings>(query, "returning *");
_ = _dispatch.Dispatch(system, guild, patch);
return settings;
}
2021-10-13 05:02:34 +00:00
public Task<MemberGuildSettings> GetMemberGuild(ulong guild, MemberId member, bool defaultInsert = true)
{
if (!defaultInsert)
return _db.QueryFirst<MemberGuildSettings>(new Query("member_guild")
.Where("guild", guild)
.Where("member", member)
);
2020-08-29 11:46:27 +00:00
var query = new Query("member_guild").AsInsert(new { guild, member });
return _db.QueryFirst<MemberGuildSettings>(query,
"on conflict (guild, member) do update set guild = $1, member = $2 returning *"
);
}
public Task<MemberGuildSettings> UpdateMemberGuild(MemberId member, ulong guild, MemberGuildPatch patch)
{
_logger.Information("Updated {MemberId} in guild {GuildId}: {@MemberGuildPatch}", member, guild, patch);
var query = patch.Apply(new Query("member_guild").Where("member", member).Where("guild", guild));
_ = _dispatch.Dispatch(member, guild, patch);
return _db.QueryFirst<MemberGuildSettings>(query, "returning *");
2020-08-29 11:46:27 +00:00
}
}