This commit is contained in:
skidoodle 2024-03-13 00:33:46 +01:00
commit e124a47765
19374 changed files with 9806149 additions and 0 deletions

View file

@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Script.Serialization;
using Kendo.Mvc;
using Kendo.Mvc.Infrastructure;
using Kendo.Mvc.UI;
using Kreta.Web.Helpers.Grid;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Kreta.Web.ModelBinder
{
public class DataSourceRequestModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
//sort = ID - asc & page = 1 & pageSize = 20 & group = &filter =
if (actionContext.Request.Method != HttpMethod.Post)
{
var request = new KretaGridDataSourceRequest();
if (TryGetValue(bindingContext, GridUrlParameters.Sort, out string sort))
{
request.Sorts = GridDescriptorSerializer.Deserialize<SortDescriptor>(sort);
}
if (TryGetValue(bindingContext, GridUrlParameters.Page, out int currentPage))
{
request.Page = currentPage;
}
if (TryGetValue(bindingContext, GridUrlParameters.PageSize, out int pageSize))
{
request.PageSize = pageSize;
}
if (TryGetValue(bindingContext, GridUrlParameters.Filter, out string filter))
{
request.Filters = FilterDescriptorFactory.Create(filter);
}
if (TryGetValue(bindingContext, GridUrlParameters.Group, out string group))
{
request.Groups = GridDescriptorSerializer.Deserialize<GroupDescriptor>(group);
}
if (TryGetValue(bindingContext, GridUrlParameters.Aggregates, out string aggregates))
{
request.Aggregates = GridDescriptorSerializer.Deserialize<AggregateDescriptor>(aggregates);
}
bindingContext.Model = request;
}
else
{
var jsonString = ExtractRequestJson(actionContext);
JObject json = (JObject)JsonConvert.DeserializeObject(jsonString);
var d = new Dictionary<string, string>(json.ToObject<IDictionary<string, string>>(), StringComparer.CurrentCultureIgnoreCase);
var request = new KretaGridDataSourceRequest();
request.Sorts = d.ContainsKey("sort")
? GridDescriptorSerializer.Deserialize<SortDescriptor>(d["sort"])
: null;
request.Page = d.ContainsKey("page") ? int.Parse(d["page"]) : 1;
request.PageSize = d.ContainsKey("pageSize") ? int.Parse(d["pageSize"]) : 0;
request.Groups = d.ContainsKey("group")
? GridDescriptorSerializer.Deserialize<GroupDescriptor>(d["group"])
: null;
request.Aggregates = d.ContainsKey("aggregates")
? GridDescriptorSerializer.Deserialize<AggregateDescriptor>(d["aggregates"])
: null;
request.Filters = d.ContainsKey("filter") ? FilterDescriptorFactory.Create(d["filter"]) : null;
request.data = d.ContainsKey("data") ? d["data"] : null;
bindingContext.Model = request;
}
return true;
}
private bool TryGetValue<T>(ModelBindingContext bindingContext, string key, out T result)
{
var value = bindingContext.ValueProvider.GetValue(key);
if (value == null)
{
result = default(T);
return false;
}
result = (T)value.ConvertTo(typeof(T));
return true;
}
private static string ExtractRequestJson(HttpActionContext actionContext)
{
var content = actionContext.Request.Content;
var query = content.ReadAsStringAsync().Result;
var dict = HttpUtility.ParseQueryString(query);
var json = new JavaScriptSerializer().Serialize(
dict.AllKeys.ToDictionary(k => k, k => dict[k]));
return json;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
namespace Kreta.Web.ModelBinder
{
public class DecimalModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(decimal?))))
{
try
{
bindingContext.Model = decimal.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
return false;
}
}
return true;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
namespace Kreta.Web.ModelBinder
{
public class DoubleModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(double?))))
{
try
{
bindingContext.Model = double.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
return false;
}
}
return true;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Http.Controllers;
using System.Web.Http.ModelBinding;
using System.Web.Http.ValueProviders;
namespace Kreta.Web.ModelBinder
{
public class IntegerModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(int?))))
{
try
{
bindingContext.Model = int.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
return false;
}
}
return true;
}
}
}

View file

@ -0,0 +1,15 @@
using System.Web.Mvc;
namespace Kreta.Web.ModelBinder.Mvc
{
public class AlapdokumentumFileUploadBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var name = controllerContext.HttpContext.Request.Files.AllKeys[0];
bindingContext.ModelName = name;
var ret = base.BindModel(controllerContext, bindingContext);
return ret;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Mvc;
namespace Kreta.Web.ModelBinder.Mvc
{
public class DecimalModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
object actualValue = null;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(decimal?))))
{
try
{
actualValue = decimal.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
}
return actualValue;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Mvc;
namespace Kreta.Web.ModelBinder.Mvc
{
public class DoubleModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
object actualValue = null;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(double?))))
{
try
{
actualValue = double.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
}
return actualValue;
}
}
}

View file

@ -0,0 +1,32 @@
using System;
using System.Globalization;
using System.Web.Mvc;
namespace Kreta.Web.ModelBinder.Mvc
{
public class IntegerModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var modelState = new ModelState { Value = valueResult };
bindingContext.ModelState[bindingContext.ModelName] = modelState;
object actualValue = null;
if ((valueResult != null) && !(string.IsNullOrWhiteSpace(valueResult.AttemptedValue) && (bindingContext.ModelType == typeof(int?))))
{
try
{
actualValue = int.Parse(valueResult.AttemptedValue, NumberStyles.Number, CultureInfo.CreateSpecificCulture("hu-HU"));
}
catch (FormatException e)
{
modelState.Errors.Add(e);
}
}
return actualValue;
}
}
}

View file

@ -0,0 +1,15 @@
using System.Web.Mvc;
namespace Kreta.Web.ModelBinder.Mvc
{
public class StringTrimModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
var attemptedValue = value?.AttemptedValue;
return string.IsNullOrWhiteSpace(attemptedValue) ? null : attemptedValue.Trim();
}
}
}