12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- using System;
- using System.Collections.Generic;
- using Luticate2.Auth.Utils.Business.Utils;
- using Luticate2.Auth.Utils.Dbo.Pagination;
- using Luticate2.Auth.Utils.Dbo.Result;
-
- namespace Luticate2.Auth.Utils.Business.Pagination
- {
- public static class LuOrderByParser
- {
- public static LuResult<LuOrderByDbo> Parse<TDbo>(IEnumerable<string> data, LuExpressionUtils.LuExpressionParserOptions options)
- {
- var orderByDbo = new LuOrderByDbo
- {
- OrderByFields = new List<LuOrderByFieldDbo>()
- };
- foreach (var orderByFieldStr in data)
- {
- var orderByFieldResult = Parse<TDbo>(orderByFieldStr, options);
- if (!orderByFieldResult)
- {
- return orderByFieldResult.To<LuOrderByDbo>();
- }
- var orderByFieldDbo = orderByFieldResult.Data;
-
- orderByDbo.OrderByFields.Add(orderByFieldDbo);
- }
-
- return LuResult<LuOrderByDbo>.Ok(orderByDbo);
- }
-
- public static LuResult<LuOrderByFieldDbo> Parse<TDbo>(string data, LuExpressionUtils.LuExpressionParserOptions options)
- {
- var orderByFieldDbo = new LuOrderByFieldDbo();
- if (data.EndsWith(":desc", StringComparison.OrdinalIgnoreCase))
- {
- orderByFieldDbo.Asc = false;
- data = data.Substring(0, data.Length - 5);
- }
- else
- {
- orderByFieldDbo.Asc = true;
-
- if (data.EndsWith(":asc", StringComparison.OrdinalIgnoreCase))
- {
- data = data.Substring(0, data.Length - 4);
- }
- }
-
- var completeData = $"(x) => ({data})";
- var expressionResult = LuExpressionUtils.Parse<TDbo, object>(completeData, options);
- if (!expressionResult)
- {
- return expressionResult.To<LuOrderByFieldDbo>();
- }
- orderByFieldDbo.Expression = expressionResult.Data;
-
- return LuResult<LuOrderByFieldDbo>.Ok(orderByFieldDbo);
- }
- }
- }
|