Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

LuOrderByParser.cs 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Collections.Generic;
  3. using Luticate2.Auth.Utils.Business.Utils;
  4. using Luticate2.Auth.Utils.Dbo.Pagination;
  5. using Luticate2.Auth.Utils.Dbo.Result;
  6. namespace Luticate2.Auth.Utils.Business.Pagination
  7. {
  8. public static class LuOrderByParser
  9. {
  10. public static LuResult<LuOrderByDbo> Parse<TDbo>(IEnumerable<string> data, LuExpressionUtils.LuExpressionParserOptions options)
  11. {
  12. var orderByDbo = new LuOrderByDbo
  13. {
  14. OrderByFields = new List<LuOrderByFieldDbo>()
  15. };
  16. foreach (var orderByFieldStr in data)
  17. {
  18. var orderByFieldResult = Parse<TDbo>(orderByFieldStr, options);
  19. if (!orderByFieldResult)
  20. {
  21. return orderByFieldResult.To<LuOrderByDbo>();
  22. }
  23. var orderByFieldDbo = orderByFieldResult.Data;
  24. orderByDbo.OrderByFields.Add(orderByFieldDbo);
  25. }
  26. return LuResult<LuOrderByDbo>.Ok(orderByDbo);
  27. }
  28. public static LuResult<LuOrderByFieldDbo> Parse<TDbo>(string data, LuExpressionUtils.LuExpressionParserOptions options)
  29. {
  30. var orderByFieldDbo = new LuOrderByFieldDbo();
  31. if (data.EndsWith(":desc", StringComparison.OrdinalIgnoreCase))
  32. {
  33. orderByFieldDbo.Asc = false;
  34. data = data.Substring(0, data.Length - 5);
  35. }
  36. else
  37. {
  38. orderByFieldDbo.Asc = true;
  39. if (data.EndsWith(":asc", StringComparison.OrdinalIgnoreCase))
  40. {
  41. data = data.Substring(0, data.Length - 4);
  42. }
  43. }
  44. var completeData = $"(x) => ({data})";
  45. var expressionResult = LuExpressionUtils.Parse<TDbo, object>(completeData, options);
  46. if (!expressionResult)
  47. {
  48. return expressionResult.To<LuOrderByFieldDbo>();
  49. }
  50. orderByFieldDbo.Expression = expressionResult.Data;
  51. return LuResult<LuOrderByFieldDbo>.Ok(orderByFieldDbo);
  52. }
  53. }
  54. }