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.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Collections.Generic;
  2. using Luticate2.Auth.Utils.Dbo;
  3. using Luticate2.Auth.Utils.Dbo.Fields;
  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(string data)
  11. {
  12. if (data == null)
  13. {
  14. data = "";
  15. }
  16. data = data.Trim();
  17. if (data != "")
  18. {
  19. var dbo = new LuOrderByDbo {OrderByFields = new List<LuOrderByFieldDbo>()};
  20. var fields = data.Split(',');
  21. foreach (var field in fields)
  22. {
  23. if (field == "")
  24. {
  25. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
  26. $"LuOrderByParser: {data}", "Empty order by field");
  27. }
  28. var orderByField = new LuOrderByFieldDbo();
  29. var split = field.Split(':');
  30. if (split.Length > 2 || split.Length == 0)
  31. {
  32. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
  33. $"LuOrderByParser: {data}", "Invalid order by field syntax");
  34. }
  35. orderByField.Field = LuFieldDbo.Make(split[0]);
  36. var order = split.Length == 2 ? split[1].ToLower() : "asc";
  37. if (order == "asc")
  38. {
  39. orderByField.Asc = true;
  40. }
  41. else if (order == "desc")
  42. {
  43. orderByField.Asc = false;
  44. }
  45. else
  46. {
  47. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
  48. $"LuOrderByParser: {data}", "Empty order by field order");
  49. }
  50. dbo.OrderByFields.Add(orderByField);
  51. }
  52. return LuResult<LuOrderByDbo>.Ok(dbo);
  53. }
  54. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(), "Order by is empty or missing", "");
  55. }
  56. }
  57. }