public async Task BindModelAsync(ModelBindingContext bindingContext) { var key = bindingContext.HttpContext.Request.Query.Keys.FirstOrDefault(x => x.StartsWith("anyof")); if (key == null) { bindingContext.Result = ModelBindingResult.Success(null); return; } var fields = key.Split(".", StringSplitOptions.RemoveEmptyEntries).Skip(1); if (fields.Count() < 2) { bindingContext.ModelState.TryAddModelError(key, "Invalid syntax of `anyof` parameter. At least two fields must be specified, e.g. `anyof.field1.field2=value`."); return; } var hasValue = false; if (!bindingContext.TryGetAccount(key, ref hasValue, out var value)) { return; } if (!hasValue) { bindingContext.Result = ModelBindingResult.Success(null); return; } bindingContext.Result = ModelBindingResult.Success(new AnyOfParameter { Fields = fields, Value = (await Accounts.GetAsync(value))?.Id ?? -1 }); }
public async Task BindModelAsync(ModelBindingContext bindingContext) { var model = bindingContext.ModelName; var hasValue = false; if (!bindingContext.TryGetAccount($"{model}", ref hasValue, out var value)) { return; } if (!bindingContext.TryGetAccount($"{model}.eq", ref hasValue, out var eq)) { return; } if (!bindingContext.TryGetAccount($"{model}.ne", ref hasValue, out var ne)) { return; } if (!bindingContext.TryGetAccountList($"{model}.in", ref hasValue, out var @in)) { return; } if (!bindingContext.TryGetAccountList($"{model}.ni", ref hasValue, out var ni)) { return; } if (!bindingContext.TryGetString($"{model}.eqx", ref hasValue, out var eqx)) { return; } if (!bindingContext.TryGetString($"{model}.nex", ref hasValue, out var nex)) { return; } if (!bindingContext.TryGetBool($"{model}.null", ref hasValue, out var isNull)) { return; } if (!hasValue) { bindingContext.Result = ModelBindingResult.Success(null); return; } int? _eq = null; int? _ne = null; List <int> _listIn = null; List <int> _listNi = null; if ((value ?? eq) != null) { _eq = (await Accounts.GetAsync(value ?? eq))?.Id ?? -1; } if (ne != null) { _ne = (await Accounts.GetAsync(ne))?.Id ?? -1; } if (@in != null) { _listIn = new List <int>(@in.Count); foreach (var addr in @in) { var acc = await Accounts.GetAsync(addr); if (acc != null) { _listIn.Add(acc.Id); } } } if (ni != null) { _listNi = new List <int>(ni.Count); foreach (var addr in ni) { var acc = await Accounts.GetAsync(addr); if (acc != null) { _listNi.Add(acc.Id); } } } bindingContext.Result = ModelBindingResult.Success(new AccountParameter { Eq = _eq, Ne = _ne, In = _listIn, Ni = _listNi, Eqx = eqx, Nex = nex, Null = isNull }); }