-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
* Create QueryChannelsAsync overload with ChannelSortObject param to control result sorting * Add comments + code samples * add missing compiler flag * Make filters parameter optional in IStreamChatClient.QueryChannelsAsync
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System.Collections.Generic; | ||
using StreamChat.Core.StatefulModels; | ||
|
||
namespace StreamChat.Core.QueryBuilders.Sort | ||
{ | ||
/// <summary> | ||
/// Factory for creating for <see cref="IStreamChannel"/> query <see cref="IStreamChatClient.QueryChannelsAsync(IDictionary{string,object}, ChannelSortObject, int, int)"/> sort object | ||
/// </summary> | ||
public static class ChannelSort | ||
{ | ||
/// <summary> | ||
/// Sort in ascending order meaning from lowest to highest value of the specified field | ||
/// </summary> | ||
/// <param name="fieldName">Field name to sort by</param> | ||
public static ChannelSortObject OrderByAscending(ChannelSortFieldName fieldName) | ||
{ | ||
var instance = new ChannelSortObject(); | ||
instance.OrderByAscending(fieldName); | ||
return instance; | ||
} | ||
|
||
/// <summary> | ||
/// Sort in descending order meaning from highest to lowest value of the specified field | ||
/// </summary> | ||
/// <param name="fieldName">Field name to sort by</param> | ||
public static ChannelSortObject OrderByDescending(ChannelSortFieldName fieldName) | ||
{ | ||
var instance = new ChannelSortObject(); | ||
instance.OrderByDescending(fieldName); | ||
return instance; | ||
} | ||
|
||
/// <summary> | ||
/// Sort in descending order meaning from highest to lowest value of the specified field | ||
/// </summary> | ||
/// <param name="fieldName">Field name to sort by</param> | ||
public static ChannelSortObject ThenByAscending(this ChannelSortObject sort, ChannelSortFieldName fieldName) | ||
=> sort.OrderByAscending(fieldName); | ||
|
||
/// <summary> | ||
/// Sort in descending order meaning from highest to lowest value of the specified field | ||
/// </summary> | ||
/// <param name="fieldName">Field name to sort by</param> | ||
public static ChannelSortObject ThenByDescending(this ChannelSortObject sort, ChannelSortFieldName fieldName) | ||
=> sort.OrderByDescending(fieldName); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using StreamChat.Core.StatefulModels; | ||
|
||
namespace StreamChat.Core.QueryBuilders.Sort | ||
{ | ||
/// <summary> | ||
/// Field names available for <see cref="IStreamChannel"/> query: <see cref="IStreamChatClient.QueryChannelsAsync(IDictionary{string,object}, ChannelSortObject, int, int)"/> | ||
/// </summary> | ||
public enum ChannelSortFieldName | ||
{ | ||
LastUpdated, | ||
LastMessageAt, | ||
UpdatedAt, | ||
CreatedAt, | ||
MemberCount, | ||
UnreadCount, | ||
HasUnread | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using StreamChat.Core.StatefulModels; | ||
|
||
namespace StreamChat.Core.QueryBuilders.Sort | ||
{ | ||
/// <summary> | ||
/// Sort object for <see cref="IStreamChannel"/> query: <see cref="IStreamChatClient.QueryChannelsAsync(IDictionary{string,object}, ChannelSortObject, int, int)"/> | ||
/// </summary> | ||
public sealed class ChannelSortObject : QuerySort<ChannelSortObject, ChannelSortFieldName> | ||
{ | ||
protected override ChannelSortObject Instance => this; | ||
|
||
protected override string ToUnderlyingFieldName(ChannelSortFieldName fieldName) | ||
{ | ||
switch (fieldName) | ||
{ | ||
case ChannelSortFieldName.LastUpdated: return "last_updated"; | ||
case ChannelSortFieldName.LastMessageAt: return "last_message_at"; | ||
case ChannelSortFieldName.UpdatedAt: return "updated_at"; | ||
case ChannelSortFieldName.CreatedAt: return "created_at"; | ||
case ChannelSortFieldName.MemberCount: return "member_count"; | ||
case ChannelSortFieldName.UnreadCount: return "unread_count "; | ||
case ChannelSortFieldName.HasUnread: return "has_unread"; | ||
default: | ||
throw new ArgumentOutOfRangeException(nameof(fieldName), fieldName, null); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System.Collections.Generic; | ||
using StreamChat.Core.InternalDTO.Requests; | ||
|
||
namespace StreamChat.Core.QueryBuilders.Sort | ||
{ | ||
/// <summary> | ||
/// Base class for query sort objects | ||
/// </summary> | ||
public abstract class QuerySort<TSortType, TFieldType> where TSortType : QuerySort<TSortType, TFieldType> | ||
{ | ||
/// <summary> | ||
/// Order by field in an ascending order | ||
/// </summary> | ||
/// <param name="fieldName"></param> | ||
/// <returns></returns> | ||
internal TSortType OrderByAscending(TFieldType fieldName) | ||
{ | ||
Instance._order.Add((fieldName, AscendingOrder)); | ||
return Instance; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="fieldName"></param> | ||
/// <returns></returns> | ||
internal TSortType OrderByDescending(TFieldType fieldName) | ||
{ | ||
Instance._order.Add((fieldName, DescendingOrder)); | ||
return Instance; | ||
} | ||
|
||
internal List<SortParamRequestInternalDTO> ToSortParamRequestList() | ||
{ | ||
if (_order.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
var sortParams = new List<SortParamRequestInternalDTO>(); | ||
|
||
foreach (var entry in _order) | ||
{ | ||
sortParams.Add(new SortParamRequestInternalDTO | ||
{ | ||
Direction = entry.Direction, | ||
Field = ToUnderlyingFieldName(entry.Field), | ||
}); | ||
} | ||
|
||
return sortParams; | ||
} | ||
|
||
protected abstract TSortType Instance { get; } | ||
|
||
protected abstract string ToUnderlyingFieldName(TFieldType field); | ||
|
||
private const int AscendingOrder = 1; | ||
private const int DescendingOrder = -1; | ||
|
||
private readonly List<(TFieldType Field, int Direction)> _order = new List<(TFieldType Field, int Direction)>(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.