private void CheckCorrectSampleJson(DataMethodViewModel model, ApiCall call) { if (model != null && model.ParameterBinding != null) { foreach (var item in model.ParameterBinding) { if (item.Key.ToLower() == Kooboo.Sites.DataSources.ScriptSourceManager.SampleResponseFieldName.ToLower()) { var value = item.Value.Binding; if (value != null) { var obj = Lib.Helper.JsonHelper.Deserialize(value); } } } } }
public List <DataSourceViewModel> Datasource(ApiCall call) { List <DataSourceViewModel> dataSourceList = new List <DataSourceViewModel>(); foreach (var item in GlobalDb.DataMethodSettings.All().GroupBy(o => o.DeclareType)) { List <DataMethodViewModel> dataSourceMethods = new List <DataMethodViewModel>(); var methodlist = item.ToList(); foreach (var methoditem in methodlist) { DataMethodViewModel onemethod = new DataMethodViewModel { Id = methoditem.Id, IsGlobal = true, IsConfigurable = methoditem.Parameters.Any(), MethodName = methoditem.MethodName, DisplayName = methoditem.DisplayName, OriginalMethodName = methoditem.OriginalMethodName, MethodSignatureHash = methoditem.MethodSignatureHash, Parameters = methoditem.Parameters }; dataSourceMethods.Add(onemethod); } DataSourceViewModel datasource = new DataSourceViewModel { Type = methodlist[0].DeclareType, IsThridPartyDataSource = methodlist[0].IsThirdPartyType, TypeHash = methodlist[0].DeclareTypeHash }; var existing = dataSourceList.Find(o => o.TypeHash == datasource.TypeHash); if (existing != null) { existing.Methods.AddRange(dataSourceMethods); } else { datasource.Methods = dataSourceMethods; dataSourceList.Add(datasource); } } return(dataSourceList); }
public DataMethodViewModel Add(ApiCall call) { DataMethodViewModel viewmodel = call.Context.Request.Model as DataMethodViewModel; if (viewmodel.IsNew) { var methodhash = viewmodel.MethodSignatureHash; IDataMethodSetting originalSetting = GlobalDb.DataMethodSettings.Query.Where(o => o.MethodSignatureHash == methodhash).FirstOrDefault(); DataMethodSetting newMethodSetting = new DataMethodSetting { MethodName = viewmodel.MethodName, ParameterBinding = viewmodel.ParameterBinding, Parameters = viewmodel.Parameters, Description = viewmodel.Description, OriginalMethodName = originalSetting.OriginalMethodName, MethodSignatureHash = originalSetting.MethodSignatureHash, DeclareType = originalSetting.DeclareType, ReturnType = originalSetting.ReturnType, DeclareTypeHash = originalSetting.DeclareTypeHash, }; if (!viewmodel.IsPublic) { newMethodSetting.MethodName = System.Guid.NewGuid().ToString(); } newMethodSetting.IsPublic = viewmodel.IsPublic; call.WebSite.SiteDb().DataMethodSettings.AddOrUpdate(newMethodSetting, call.Context.User.Id); viewmodel = UpdateViewModel(viewmodel, call.WebSite.SiteDb(), newMethodSetting); } else { var dataMethodSetting = call.WebSite.SiteDb().DataMethodSettings.Get(viewmodel.Id); dataMethodSetting.ParameterBinding = viewmodel.ParameterBinding; dataMethodSetting.Description = viewmodel.Description; call.WebSite.SiteDb().DataMethodSettings.AddOrUpdate(dataMethodSetting, call.Context.User.Id); viewmodel = UpdateViewModel(viewmodel, call.WebSite.SiteDb(), dataMethodSetting); } return(viewmodel); }
private DataMethodViewModel UpdateViewModel(DataMethodViewModel model, SiteDb siteDb, IDataMethodSetting setting) { var typeInfo = DataSourceHelper.GetFields(siteDb, setting); model.IsGlobal = setting.IsGlobal; model.MethodName = setting.MethodName; model.ParameterBinding = setting.ParameterBinding; model.Description = setting.Description; model.Parameters = setting.Parameters; model.Id = setting.Id; model.OriginalMethodName = setting.OriginalMethodName; model.MethodSignatureHash = setting.MethodSignatureHash; model.DeclareType = setting.DeclareType; model.IsGlobal = setting.IsGlobal; model.ReturnType = setting.ReturnType; model.IsPagedResult = setting.IsPagedResult; model.ItemFields = typeInfo.ItemFields; model.Enumerable = typeInfo.Enumerable; model.IsPublic = setting.IsPublic; return(model); }
public TypeInfoModel Update(ApiCall call) { string json = call.Context.Request.Body; var sitedb = call.Context.WebSite.SiteDb(); DataMethodViewModel viewmodel = Lib.Helper.JsonHelper.Deserialize <DataMethodViewModel>(json); CheckCorrectSampleJson(viewmodel, call); if (viewmodel.Id == default(Guid)) { // TODO: check if it is from the kscript source as well.. var methodhash = viewmodel.MethodSignatureHash; IDataMethodSetting originalSetting; originalSetting = GlobalDb.DataMethodSettings.Query.Where(o => o.MethodSignatureHash == methodhash).FirstOrDefault(); if (originalSetting == null) { originalSetting = Kooboo.Sites.DataSources.ScriptSourceManager.GetByMethodHash(sitedb, methodhash); } if (originalSetting != null) { DataMethodSetting newMethodSetting = new DataMethodSetting { MethodName = viewmodel.MethodName, ParameterBinding = viewmodel.ParameterBinding, Parameters = viewmodel.Parameters, Description = viewmodel.Description, OriginalMethodName = originalSetting.OriginalMethodName, MethodSignatureHash = originalSetting.MethodSignatureHash, DeclareType = originalSetting.DeclareType, ReturnType = originalSetting.ReturnType, DeclareTypeHash = originalSetting.DeclareTypeHash, IsPagedResult = originalSetting.IsPagedResult }; if (!viewmodel.IsPublic) { newMethodSetting.MethodName = System.Guid.NewGuid().ToString(); } newMethodSetting.IsPublic = viewmodel.IsPublic; call.WebSite.SiteDb().DataMethodSettings.AddOrUpdate(newMethodSetting, call.Context.User.Id); var fields = DataSourceHelper.GetFields(call.WebSite.SiteDb(), newMethodSetting); fields.Paras = GetBindingPara(newMethodSetting); return(fields); } } else { var dataMethodSetting = call.WebSite.SiteDb().DataMethodSettings.Get(viewmodel.Id); if (dataMethodSetting == null) { dataMethodSetting = Kooboo.Sites.DataSources.ScriptSourceManager.Get(call.WebSite.SiteDb(), viewmodel.Id); dataMethodSetting.ParameterBinding = viewmodel.ParameterBinding; dataMethodSetting.Description = viewmodel.Description; dataMethodSetting.IsPublic = viewmodel.IsPublic; call.WebSite.SiteDb().DataMethodSettings.AddOrUpdate(dataMethodSetting, call.Context.User.Id); var fields = DataSourceHelper.GetFields(call.WebSite.SiteDb(), dataMethodSetting); fields.Paras = GetBindingPara(dataMethodSetting); return(fields); } else { dataMethodSetting.ParameterBinding = viewmodel.ParameterBinding; dataMethodSetting.Description = viewmodel.Description; dataMethodSetting.IsPublic = viewmodel.IsPublic; call.WebSite.SiteDb().DataMethodSettings.AddOrUpdate(dataMethodSetting, call.Context.User.Id); var fields = DataSourceHelper.GetFields(call.WebSite.SiteDb(), dataMethodSetting); fields.Paras = GetBindingPara(dataMethodSetting); return(fields); } } return(null); }