| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Luticate2.Utils.Dbo.Basic;
using Luticate2.Utils.Dbo.FsFiles;
using Luticate2.Utils.Dbo.OrderBy;
using Luticate2.Utils.Dbo.PaginatedRequest;
namespace Luticate2.Utils.Utils
{
    public static class LuExtensions
    {
        public static LuFsFilesDbo ToDbo(this FileInfo file)
        {
            if (file == null)
            {
                return null;
            }
            return new LuFsFilesDbo
            {
                Id = file.Name,
                Path = file.Name,
                CreatedAt = file.CreationTime.ToDbo(),
                UpdatedAt = file.LastWriteTime.ToDbo(),
                Size = file.Length
            };
        }
        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, LuOrderByDbo orderBy,
            Func<string, Func<T, object>> getOrderByFieldExpression)
        {
            IOrderedEnumerable<T> ordered = null;
            foreach (var field in orderBy.Fields)
            {
                var exp = getOrderByFieldExpression(field.Name);
                if (ordered != null)
                {
                    ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
                }
                else
                {
                    ordered = field.Asc ? list.OrderBy(exp) : list.OrderByDescending(exp);
                }
            }
            return ordered ?? list;
        }
        public static LuPaginatedDbo<T> Paginate<T>(this IEnumerable<T> list, LuPaginatedRequestDbo paginatedRequestDbo,
            Func<string, Func<T, object>> getOrderByFieldExpression)
        {
            var ordered = list.OrderBy(paginatedRequestDbo.OrderBy, getOrderByFieldExpression).ToList();
            return new LuPaginatedDbo<T>
            {
                Count = ordered.Count,
                Data = ordered.Skip(paginatedRequestDbo.Page * paginatedRequestDbo.PerPage).Take(paginatedRequestDbo.PerPage).ToList()
            };
        }
    }
}
 |