PluralKit/PluralKit.Core/Database/Views/DatabaseViewsExt.cs
Ske 6d06474d26 Refactor sort/filter code once again
Now we handle sorting on the bot side, but still filter in the database
2020-06-13 21:49:31 +02:00

34 lines
1.4 KiB
C#

#nullable enable
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dapper;
namespace PluralKit.Core
{
public static class DatabaseViewsExt
{
public static Task<IEnumerable<SystemFronter>> QueryCurrentFronters(this IPKConnection conn, int system) =>
conn.QueryAsync<SystemFronter>("select * from system_fronters where system = @system", new {system});
public static Task<IEnumerable<ListedMember>> QueryMemberList(this IPKConnection conn, int system, PrivacyLevel? privacyFilter = null, string? filter = null, bool includeDescriptionInNameFilter = false)
{
StringBuilder query = new StringBuilder("select * from member_list where system = @system");
if (privacyFilter != null)
query.Append($" and member_privacy = {(int) privacyFilter}");
if (filter != null)
{
static string Filter(string column) => $"position(lower(@filter) in lower(coalesce({column}, ''))) > 0";
query.Append($" and ({Filter("name")} or {Filter("display_name")}");
if (includeDescriptionInNameFilter) query.Append($" or {Filter("description")}");
query.Append(")");
}
return conn.QueryAsync<ListedMember>(query.ToString(), new {system, filter});
}
}
}