diff --git a/TShockAPI/DB/BanManager.cs b/TShockAPI/DB/BanManager.cs
index 757cce81..4ffa7566 100644
--- a/TShockAPI/DB/BanManager.cs
+++ b/TShockAPI/DB/BanManager.cs
@@ -17,6 +17,7 @@ along with this program. If not, see .
*/
using System;
+using System.Linq;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
@@ -85,9 +86,16 @@ namespace TShockAPI.DB
}
///
- /// Gets a list of bans.
+ /// Gets a list of bans sorted by their addition date from newest to oldest
///
- public List GetBans()
+ public List GetBans() => GetSortedBans(BanSortMethod.AddedNewestToOldest).ToList();
+
+ ///
+ /// Retrieves an enumerable of objects, sorted using the provided sort method
+ ///
+ ///
+ ///
+ public IEnumerable GetSortedBans(BanSortMethod sortMethod)
{
List banlist = new List();
try
@@ -96,8 +104,28 @@ namespace TShockAPI.DB
{
while (reader.Read())
{
- banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")));
+ banlist.Add(new Ban(reader.Get("IP"), reader.Get("Name"), reader.Get("UUID"), reader.Get("Reason"), reader.Get("BanningUser"), reader.Get("Date"), reader.Get("Expiration")));
}
+
+ Comparison comparer;
+ switch (sortMethod)
+ {
+ case BanSortMethod.AddedOldestToNewest:
+ comparer = (a, b) => b.DateTime.CompareTo(a.DateTime);
+ break;
+ case BanSortMethod.AddedNewestToOldest:
+ comparer = (a, b) => a.DateTime.CompareTo(b.DateTime);
+ break;
+ case BanSortMethod.ExpirationLatestToSoonest:
+ comparer = (a, b) => b.ExpirationDateTime.CompareTo(a.ExpirationDateTime);
+ break;
+ default:
+ //Default encompasses BanSortMethod.ExpirationSoonestToLatest
+ comparer = (a, b) => a.ExpirationDateTime.CompareTo(b.ExpirationDateTime);
+ break;
+ }
+
+ banlist.Sort(comparer);
return banlist;
}
}
@@ -239,6 +267,29 @@ namespace TShockAPI.DB
}
}
+ ///
+ /// Enum containing sort options for ban retrieval
+ ///
+ public enum BanSortMethod
+ {
+ ///
+ /// Bans will be sorted on expiration date, from soonest to latest
+ ///
+ ExpirationSoonestToLatest,
+ ///
+ /// Bans will be sorted on expiration date, from latest to soonest
+ ///
+ ExpirationLatestToSoonest,
+ ///
+ /// Bans will be sorted by the date they were added, from newest to oldest
+ ///
+ AddedNewestToOldest,
+ ///
+ /// Bans will be sorted by the date they were added, from oldest to newest
+ ///
+ AddedOldestToNewest
+ }
+
///
/// Model class that represents a ban entry in the TShock database.
///
@@ -280,12 +331,22 @@ namespace TShockAPI.DB
/// The date, which must be in UTC
public string Date { get; set; }
+ ///
+ /// Gets the object representation of the string.
+ ///
+ public DateTime DateTime { get; }
+
///
/// Gets or sets the expiration date, in which the ban shall be lifted
///
/// The expiration.
public string Expiration { get; set; }
+ ///
+ /// Gets the object representation of the string.
+ ///
+ public DateTime ExpirationDateTime { get; }
+
///
/// Initializes a new instance of the class.
///