12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using System.Collections.Generic;
- using Luticate2.Auth.Utils.Dbo;
- using Luticate2.Auth.Utils.Dbo.Fields;
- 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(string data)
- {
- if (data == null)
- {
- data = "";
- }
- data = data.Trim();
- if (data != "")
- {
- var dbo = new LuOrderByDbo {OrderByFields = new List<LuOrderByFieldDbo>()};
- var fields = data.Split(',');
- foreach (var field in fields)
- {
- if (field == "")
- {
- return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
- $"LuOrderByParser: {data}", "Empty order by field");
- }
- var orderByField = new LuOrderByFieldDbo();
- var split = field.Split(':');
- if (split.Length > 2 || split.Length == 0)
- {
- return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
- $"LuOrderByParser: {data}", "Invalid order by field syntax");
- }
- orderByField.Field = LuFieldDbo.Make(split[0]);
- var order = split.Length == 2 ? split[1].ToLower() : "asc";
- if (order == "asc")
- {
- orderByField.Asc = true;
- }
- else if (order == "desc")
- {
- orderByField.Asc = false;
- }
- else
- {
- return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(),
- $"LuOrderByParser: {data}", "Empty order by field order");
- }
- dbo.OrderByFields.Add(orderByField);
- }
- return LuResult<LuOrderByDbo>.Ok(dbo);
- }
- return LuResult<LuOrderByDbo>.Error(LuStatus.InputError.ToInt(), "Order by is empty or missing", "");
- }
- }
- }
|