using System; using System.Collections.Generic; namespace zero.Core.Entities { /// /// Represents a paged result for a model collection /// public class ListResult { public ListResult(long totalItems, long page, long pageSize) { TotalItems = totalItems; Page = page; PageSize = pageSize; if (pageSize > 0) { TotalPages = (long)Math.Ceiling(totalItems / (decimal)pageSize); } else { TotalPages = 1; } } public ListResult(IEnumerable items, long totalItems, long pageNumber, long pageSize) : this(totalItems, pageNumber, pageSize) { Items = items; } public long Page { get; private set; } public long PageSize { get; private set; } public long TotalPages { get; private set; } public long TotalItems { get; private set; } public IEnumerable Items { get; set; } public List Statistics { get; set; } = new List(); /// /// Calculates the skip size based on the paged parameters specified /// /// /// Returns 0 if the page number or page size is zero /// public int GetSkipSize() { if (Page > 0 && PageSize > 0) { return Convert.ToInt32((Page - 1) * PageSize); } return 0; } /// /// Adds a statistic value /// public ListResult AddStatistic(string key, object value, StatisticDisplay display = StatisticDisplay.Default) { Statistics.Add(new PagedResultStatistic() { Key = key, Value = value, Display = display }); return this; } } public class PagedResultStatistic { public string Key { get; set; } public object Value { get; set; } public StatisticDisplay Display { get; set; } } public enum StatisticDisplay { Default, Currency } public enum Direction { Ascending = 0, Descending = 1 } }