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

89 lines
4.3 KiB
C#
Raw Normal View History

2021-08-27 15:03:47 +00:00
#nullable enable
2021-09-27 03:18:17 +00:00
using System;
2020-08-29 11:46:27 +00:00
using System.Collections.Generic;
using System.Data;
2020-08-29 11:46:27 +00:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public partial class ModelRepository
{
public Task<PKGroup?> GetGroupByName(IPKConnection conn, SystemId system, string name) =>
2021-08-27 15:03:47 +00:00
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(Name) = lower(@Name)", new { System = system, Name = name });
public Task<PKGroup?> GetGroupByDisplayName(IPKConnection conn, SystemId system, string display_name) =>
2021-08-27 15:03:47 +00:00
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where system = @System and lower(display_name) = lower(@Name)", new { System = system, Name = display_name });
public Task<PKGroup?> GetGroupByHid(IPKConnection conn, string hid, SystemId? system = null)
=> conn.QueryFirstOrDefaultAsync<PKGroup?>(
"select * from groups where hid = @hid" + (system != null ? " and system = @System" : ""),
new { hid = hid.ToLowerInvariant(), System = system }
);
2021-08-27 15:03:47 +00:00
2021-09-27 03:18:17 +00:00
public Task<PKGroup?> GetGroupByGuid(IPKConnection conn, Guid guid) =>
conn.QueryFirstOrDefaultAsync<PKGroup?>("select * from groups where uuid = @Uuid", new { Uuid = guid });
2020-08-29 11:46:27 +00:00
public Task<int> GetGroupMemberCount(IPKConnection conn, GroupId id, PrivacyLevel? privacyFilter = null)
{
var query = new StringBuilder("select count(*) from group_members");
if (privacyFilter != null)
query.Append(" inner join members on group_members.member_id = members.id");
query.Append(" where group_members.group_id = @Id");
if (privacyFilter != null)
query.Append(" and members.member_visibility = @PrivacyFilter");
2021-08-27 15:03:47 +00:00
return conn.QuerySingleOrDefaultAsync<int>(query.ToString(), new { Id = id, PrivacyFilter = privacyFilter });
2020-08-29 11:46:27 +00:00
}
2021-08-27 15:03:47 +00:00
public async Task<PKGroup> CreateGroup(IPKConnection conn, SystemId system, string name, IDbTransaction? transaction = null)
2020-08-29 11:46:27 +00:00
{
var group = await conn.QueryFirstAsync<PKGroup>(
"insert into groups (hid, system, name) values (find_free_group_hid(), @System, @Name) returning *",
2021-08-27 15:03:47 +00:00
new { System = system, Name = name }, transaction);
2020-08-29 11:46:27 +00:00
_logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
return group;
}
public Task<PKGroup> UpdateGroup(IPKConnection conn, GroupId id, GroupPatch patch, IDbTransaction? transaction = null)
2020-08-29 11:46:27 +00:00
{
_logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch);
var (query, pms) = patch.Apply(UpdateQueryBuilder.Update("groups", "id = @id"))
.WithConstant("id", id)
.Build("returning *");
return conn.QueryFirstAsync<PKGroup>(query, pms, transaction);
2020-08-29 11:46:27 +00:00
}
public Task DeleteGroup(IPKConnection conn, GroupId group)
{
_logger.Information("Deleted {GroupId}", group);
2021-08-27 15:03:47 +00:00
return conn.ExecuteAsync("delete from groups where id = @Id", new { Id = @group });
2020-08-29 11:46:27 +00:00
}
public async Task AddMembersToGroup(IPKConnection conn, GroupId group,
IReadOnlyCollection<MemberId> members)
{
await using var w =
conn.BeginBinaryImport("copy group_members (group_id, member_id) from stdin (format binary)");
foreach (var member in members)
{
await w.StartRowAsync();
await w.WriteAsync(group.Value);
await w.WriteAsync(member.Value);
}
await w.CompleteAsync();
_logger.Information("Added members to {GroupId}: {MemberIds}", group, members);
}
public Task RemoveMembersFromGroup(IPKConnection conn, GroupId group,
IReadOnlyCollection<MemberId> members)
{
_logger.Information("Removed members from {GroupId}: {MemberIds}", group, members);
return conn.ExecuteAsync("delete from group_members where group_id = @Group and member_id = any(@Members)",
2021-08-27 15:03:47 +00:00
new { Group = @group, Members = members.ToArray() });
2020-08-29 11:46:27 +00:00
}
}
}