using System; using System.Linq.Expressions; using Luticate2.Auth.Business.Fields; using Luticate2.Auth.Dbo; using Luticate2.Auth.Dbo.Fields; using Luticate2.Auth.Dbo.Result; using Luticate2.Auth.Interfaces; using Microsoft.Extensions.DependencyInjection; namespace Luticate2.Auth.Business.FieldsExpressions { public class LuFieldsExpressions : ILuFieldsExpressions { protected IServiceProvider ServiceProvider { get; } public LuFieldsExpressions(IServiceProvider serviceProvider) { ServiceProvider = serviceProvider; } public ILuFieldsExpressions GetService() { var service = ServiceProvider.GetService>(); return service; } protected LuResult>> GetSubField( Expression> modelProperty, LuFieldDbo field, Expression> modelSubProperty) { var subPropExprResult = LuExpressionUtils.GetSingleMemberFromExpression(modelSubProperty); if (subPropExprResult == null) { return LuResult>>.Error(LuStatus.InternalError.ToInt(), "Bad field expression"); } var prop = Expression.Property(modelProperty.Body, subPropExprResult.Name); var exp = Expression.Lambda>(prop, modelProperty.Parameters[0]); var service = GetService(); var finalExpResult = service.GetExpression(exp, field.Popped()); return finalExpResult; } protected virtual LuResult>> GetExpressionInternal( Expression> modelProperty, LuFieldDbo field) { return LuResult>>.Error(LuStatus.InputError.ToInt(), $"Unknown field: {field}", ""); } public virtual LuResult>> GetExpression(Expression> modelProperty, LuFieldDbo field) { if (field.IsRoot()) { var converted = Expression.Convert(modelProperty.Body, typeof(object)); var exp = Expression.Lambda>(converted, modelProperty.Parameters[0]); return LuResult>>.Ok(exp); } return GetExpressionInternal(modelProperty, field); } } }