Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LuExtensions.cs 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using Luticate2.Utils.Dbo.Basic;
  6. using Luticate2.Utils.Dbo.FsFiles;
  7. using Luticate2.Utils.Dbo.OrderBy;
  8. using Luticate2.Utils.Dbo.PaginatedRequest;
  9. namespace Luticate2.Utils.Utils
  10. {
  11. public static class LuExtensions
  12. {
  13. public static LuFsFilesDbo ToDbo(this FileInfo file)
  14. {
  15. if (file == null)
  16. {
  17. return null;
  18. }
  19. return new LuFsFilesDbo
  20. {
  21. Id = file.Name,
  22. Path = file.Name,
  23. CreatedAt = file.CreationTime.ToDbo(),
  24. UpdatedAt = file.LastWriteTime.ToDbo(),
  25. Size = file.Length
  26. };
  27. }
  28. public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, LuOrderByDbo orderBy,
  29. Func<string, Func<T, object>> getOrderByFieldExpression)
  30. {
  31. IOrderedEnumerable<T> ordered = null;
  32. foreach (var field in orderBy.Fields)
  33. {
  34. var exp = getOrderByFieldExpression(field.Name);
  35. if (ordered != null)
  36. {
  37. ordered = field.Asc ? ordered.ThenBy(exp) : ordered.ThenByDescending(exp);
  38. }
  39. else
  40. {
  41. ordered = field.Asc ? list.OrderBy(exp) : list.OrderByDescending(exp);
  42. }
  43. }
  44. return ordered ?? list;
  45. }
  46. public static LuPaginatedDbo<T> Paginate<T>(this IEnumerable<T> list, LuPaginatedRequestDbo paginatedRequestDbo,
  47. Func<string, Func<T, object>> getOrderByFieldExpression)
  48. {
  49. var ordered = list.OrderBy(paginatedRequestDbo.OrderBy, getOrderByFieldExpression).ToList();
  50. return new LuPaginatedDbo<T>
  51. {
  52. Count = ordered.Count,
  53. Data = ordered.Skip(paginatedRequestDbo.Page * paginatedRequestDbo.PerPage).Take(paginatedRequestDbo.PerPage).ToList()
  54. };
  55. }
  56. }
  57. }