aurora/aurora-shared/Cursor/Cursor.cs

125 lines
3.6 KiB
C#
Raw Normal View History

2021-03-05 16:56:51 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
namespace Aurora.Cursor
{
public enum SortDirection
{
Asc,
Desc,
}
public class Cursor<T> where T : ICursorObject
{
private CursorList<T> _list;
private string _previousPageToken;
private string _nextPageToken;
2021-03-05 16:56:51 +00:00
private int _pageSize;
private Func<KeyValuePair<string, T>, string> _sortDelgate;
private SortDirection _direction;
public Cursor(ref CursorList<T> list)
{
this._list = list;
this._previousPageToken = string.Empty;
this._nextPageToken = string.Empty;
this._pageSize = 10;
this._sortDelgate = delegate (KeyValuePair<string, T> item)
{
2021-03-05 21:13:25 +00:00
return item.Key;
2021-03-05 16:56:51 +00:00
};
}
public CursorResult<T> GetNextPage()
{
2021-03-05 19:30:25 +00:00
if (this._pageSize < 0)
{
throw new InvalidOperationException("Page Size must be greater than zero");
}
2021-03-05 16:56:51 +00:00
List<KeyValuePair<string, T>> tmpList;
// Sort reference list
if (this._direction == SortDirection.Desc)
{
tmpList = this._list.OrderByDescending(this._sortDelgate).ToList();
}
else
{
tmpList = this._list.OrderBy(this._sortDelgate).ToList();
}
if (tmpList == null)
{
throw new System.NullReferenceException();
}
int startIdx = 0;
if (!string.IsNullOrEmpty(this._nextPageToken))
{
// TODO find another way to index into the list that's not a regular array search
2021-03-05 21:13:25 +00:00
startIdx = tmpList.FindIndex(item => item.Key == this._nextPageToken) + 1;
}
int adjustedSize = this._pageSize;
if (startIdx + this._pageSize > tmpList.Count)
{
adjustedSize = this._pageSize - ((startIdx + _pageSize) - tmpList.Count);
2021-03-05 16:56:51 +00:00
}
2021-03-05 21:13:25 +00:00
List<KeyValuePair<string, T>> selection = new List<KeyValuePair<string, T>>();
if (adjustedSize != 0)
{
selection = tmpList.GetRange(startIdx, adjustedSize);
}
2021-03-05 16:56:51 +00:00
return new CursorResult<T>
{
2021-03-05 21:13:25 +00:00
NextPageToken = this._pageSize == selection.Count ? selection[selection.Count - 1].Key : string.Empty,
PrevPageToken = string.Empty,
2021-03-05 16:56:51 +00:00
Count = this._list.Count,
2021-03-05 19:30:25 +00:00
Result = selection.ConvertAll(item => item.Value)
2021-03-05 16:56:51 +00:00
};
}
public CursorResult<T> GetPreviousPage()
{
throw new NotImplementedException();
}
2021-03-05 19:30:25 +00:00
public Cursor<T> WithNextPageToken(string nextPageToken)
2021-03-05 16:56:51 +00:00
{
this._nextPageToken = nextPageToken;
return this;
}
2021-03-05 19:30:25 +00:00
public Cursor<T> WithPreviousPageToken(string prevPageToken)
2021-03-05 16:56:51 +00:00
{
this._previousPageToken = prevPageToken;
return this;
}
public Cursor<T> WithSort(Func<KeyValuePair<string, T>, string> sortDelegate, SortDirection direction)
{
this._sortDelgate = sortDelegate;
this._direction = direction;
return this;
}
2021-03-06 00:09:42 +00:00
public Cursor<T> WithSort(string key, SortDirection direction)
{
this._sortDelgate = (item) => key;
this._direction = direction;
return this;
}
2021-03-05 16:56:51 +00:00
public Cursor<T> WithSize(int size)
{
this._pageSize = size;
return this;
}
}
}