您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LuOrderByBinder.cs 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Collections.Generic;
  2. using System.Threading.Tasks;
  3. using Luticate2.Utils.Dbo.Result;
  4. using Microsoft.AspNetCore.Mvc.ModelBinding;
  5. namespace Luticate2.Utils.Dbo.OrderBy
  6. {
  7. public class LuOrderByBinder : IModelBinder
  8. {
  9. public static LuResult<LuOrderByDbo> FromString(string data)
  10. {
  11. if (data == null)
  12. {
  13. data = "";
  14. }
  15. data = data.Trim();
  16. var dbo = new LuOrderByDbo {Fields = new List<LuOrderByFieldDbo>()};
  17. if (data != "")
  18. {
  19. var fields = data.Split(',');
  20. foreach (var field in fields)
  21. {
  22. if (field == "")
  23. {
  24. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
  25. string.Format("LuOrderByBinder: {0}", data), "Empty order by field");
  26. }
  27. var value = new LuOrderByFieldDbo();
  28. var split = field.Split(':');
  29. if (split.Length > 2 || split.Length == 0)
  30. {
  31. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
  32. string.Format("LuOrderByBinder: {0}", data), "Invalid field syntax");
  33. }
  34. value.Name = split[0];
  35. var order = split.Length == 2 ? split[1].ToLower() : "asc";
  36. if (order == "asc")
  37. {
  38. value.Asc = true;
  39. }
  40. else if (order == "desc")
  41. {
  42. value.Asc = false;
  43. }
  44. else
  45. {
  46. return LuResult<LuOrderByDbo>.Error(LuStatus.InputError,
  47. string.Format("LuOrderByBinder: {0}", data), "Empty order by field order");
  48. }
  49. dbo.Fields.Add(value);
  50. }
  51. }
  52. return LuResult<LuOrderByDbo>.Ok(dbo);
  53. }
  54. public Task BindModelAsync(ModelBindingContext bindingContext)
  55. {
  56. var messageTypeResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
  57. var res = FromString(messageTypeResult.FirstValue);
  58. if (res)
  59. {
  60. bindingContext.Result = ModelBindingResult.Success(res.Data);
  61. }
  62. else
  63. {
  64. bindingContext.Result = ModelBindingResult.Failed();
  65. }
  66. return Task.FromResult(0);
  67. }
  68. }
  69. public class LuOrderByBinderProvider : IModelBinderProvider
  70. {
  71. public IModelBinder GetBinder(ModelBinderProviderContext context)
  72. {
  73. if (context.Metadata.ModelType == typeof(LuOrderByDbo))
  74. {
  75. return new LuOrderByBinder();
  76. }
  77. return null;
  78. }
  79. }
  80. }