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

77 lines
2.8 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.Threading.Tasks;
using SqlKata;
2020-08-29 11:46:27 +00:00
namespace PluralKit.Core
{
public partial class ModelRepository
{
public Task<PKGroup?> GetGroupByName(SystemId system, string name)
{
var query = new Query("groups").Where("system", system).WhereRaw("lower(name) = lower(?)", name.ToLower());
return _db.QueryFirst<PKGroup?>(query);
}
2021-09-27 03:18:17 +00:00
public Task<PKGroup?> GetGroupByDisplayName(SystemId system, string display_name)
2020-08-29 11:46:27 +00:00
{
var query = new Query("groups").Where("system", system).WhereRaw("lower(display_name) = lower(?)", display_name.ToLower());
return _db.QueryFirst<PKGroup?>(query);
2020-08-29 11:46:27 +00:00
}
2021-08-27 15:03:47 +00:00
public Task<PKGroup?> GetGroupByHid(string hid, SystemId? system = null)
2020-08-29 11:46:27 +00:00
{
var query = new Query("groups").Where("hid", hid.ToLower());
if (system != null)
query = query.Where("system", system);
return _db.QueryFirst<PKGroup?>(query);
2020-08-29 11:46:27 +00:00
}
public Task<PKGroup?> GetGroupByGuid(Guid uuid)
2020-08-29 11:46:27 +00:00
{
var query = new Query("groups").Where("uuid", uuid);
return _db.QueryFirst<PKGroup?>(query);
2020-08-29 11:46:27 +00:00
}
public Task<int> GetGroupMemberCount(GroupId id, PrivacyLevel? privacyFilter = null)
2020-08-29 11:46:27 +00:00
{
var query = new Query("group_members")
.SelectRaw("count(*)")
.Where("group_members.group_id", id);
if (privacyFilter != null) query = query
.Join("members", "group_members.member_id", "members.id")
.Where("members.member_visibility", privacyFilter);
return _db.QueryFirst<int>(query);
2020-08-29 11:46:27 +00:00
}
public async Task<PKGroup> CreateGroup(SystemId system, string name, IPKConnection? conn = null)
2020-08-29 11:46:27 +00:00
{
var query = new Query("groups").AsInsert(new
2020-08-29 11:46:27 +00:00
{
hid = new UnsafeLiteral("find_free_group_hid()"),
system = system,
name = name
});
var group = await _db.QueryFirst<PKGroup>(conn, query, extraSql: "returning *");
_logger.Information("Created group {GroupId} in system {SystemId}: {GroupName}", group.Id, system, name);
return group;
}
2020-08-29 11:46:27 +00:00
public Task<PKGroup> UpdateGroup(GroupId id, GroupPatch patch, IPKConnection? conn = null)
{
_logger.Information("Updated {GroupId}: {@GroupPatch}", id, patch);
var query = patch.Apply(new Query("groups").Where("id", id));
return _db.QueryFirst<PKGroup>(conn, query, extraSql: "returning *");
2020-08-29 11:46:27 +00:00
}
public Task DeleteGroup(GroupId group)
2020-08-29 11:46:27 +00:00
{
_logger.Information("Deleted {GroupId}", group);
var query = new Query("groups").AsDelete().Where("id", group);
return _db.ExecuteQuery(query);
2020-08-29 11:46:27 +00:00
}
}
}