using Umbraco.Core.Persistence.DatabaseModelDefinitions;
namespace Umbraco.Core.Services
{
///
/// Represents ordering information.
///
public class Ordering
{
private static readonly Ordering DefaultOrdering = new Ordering(null);
///
/// Initializes a new instance of the class.
///
/// The name of the ordering field.
/// The ordering direction.
/// The (ISO) culture to consider when sorting multi-lingual fields.
/// A value indicating whether the ordering field is a custom user property.
///
/// The can be null, meaning: not sorting. If it is the empty string, it becomes null.
/// The can be the empty string, meaning: invariant. If it is null, it becomes the empty string.
///
public Ordering(string orderBy, Direction direction = Direction.Ascending, string culture = null, bool isCustomField = false)
{
OrderBy = orderBy.IfNullOrWhiteSpace(null); // empty is null and means, not sorting
Direction = direction;
Culture = culture.IfNullOrWhiteSpace(string.Empty); // empty is "" and means, invariant
IsCustomField = isCustomField;
}
///
/// Creates a new instance of the class.
///
/// The name of the ordering field.
/// The ordering direction.
/// The (ISO) culture to consider when sorting multi-lingual fields.
/// A value indicating whether the ordering field is a custom user property.
///
/// The can be null, meaning: not sorting. If it is the empty string, it becomes null.
/// The can be the empty string, meaning: invariant. If it is null, it becomes the empty string.
///
public static Ordering By(string orderBy, Direction direction = Direction.Ascending, string culture = null, bool isCustomField = false)
=> new Ordering(orderBy, direction, culture, isCustomField);
///
/// Gets the default instance.
///
public static Ordering ByDefault()
=> DefaultOrdering;
///
/// Gets the name of the ordering field.
///
public string OrderBy { get; }
///
/// Gets the ordering direction.
///
public Direction Direction { get; }
///
/// Gets (ISO) culture to consider when sorting multi-lingual fields.
///
public string Culture { get; }
///
/// Gets a value indicating whether the ordering field is a custom user property.
///
public bool IsCustomField { get; }
///
/// Gets a value indicating whether this ordering is the default ordering.
///
public bool IsEmpty => this == DefaultOrdering || OrderBy == null;
///
/// Gets a value indicating whether the culture of this ordering is invariant.
///
public bool IsInvariant => this == DefaultOrdering || Culture == string.Empty;
}
}